I am trying to map different items using the pk(primary key). I have used it in my urls.py, views.py as well as the templates file. But I am a NoReverseMatch Error. I have also tried changing the str to int in the urls.py file but it still gave the same error. I hope I could get someone to kindly help. Thanks
error log
Environment:
Request Method: GET
Request URL: http://localhost:8000/update-reservation/1/
Django Version: 3.1.4
Python Version: 3.7.3
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'core',
'dashboard',
'widget_tweaks',
'phonenumber_field']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Template error:
In template C:\Users\Habib\Documents\django\FIVERR\adam_mailk\templates\dashboard\super\admin\base.html, error at line 41
Reverse for 'updatebar' with keyword arguments '{'updatebar': ''}' not found. 1 pattern(s) tried: ['updatebar/(?P<pk>[^/]+)/$']
31 : <aside class="left-panel">
32 : <div class="logo">
33 : <a href="{% url 'dashboard' %}" class="logo-expanded">
34 : <img src="{% static 'logo.png' %}" style="background-color: white; width: 140px; height: 65px;" alt="logo">
35 : </a>
36 : </div>
37 : <nav class="navigation">
38 : <ul class="list-unstyled">
39 : <li {% if request.path == '/dashboard/' %} class="active" {% endif %}><a href="{% url 'dashboard' %}"><i class="ion-home"></i> <span class="nav-label">Dashboard</span></a>
40 : </li>
41 : <li {% if request.path == '/updatebar/' %}class="active"{% endif %}><a href=" {% url 'updatebar' updatebar=updatebar.pk %} "><i class="ion-plus-round"></i> <span class="nav-label">Create Table</span></a>
42 : </li>
43 : <li {% if request.path == '/add-reservation/' %}class="active"{% endif %}><a href="{% url 'add' %}"><i class="ion-plus-round"></i> <span class="nav-label">Add Reservations</span></a>
44 : </li>
45 : <li {% if request.path == '/view-reservations/' %}class="active" {% endif %}><a href="{% url 'view_reservations' %}"><i class="ion-folder"></i> <span class="nav-label">View Reservations</span></a>
46 : </li>
47 : <li {% if request.path == '/profile/' %}class="active"{% endif %}><a href="{% url 'profile' %}"><i class="ion-person"></i> <span class="nav-label">Profile</span></a></li>
48 : <li class=""><a href="{% url 'logout' %}"><i class="ion-log-out "></i> <span class="nav-label">Logout</span></a>
49 : </li>
50 : </nav>
51 : </aside>
Traceback (most recent call last):
File "C:\Users\Habib\Documents\django\FIVERR\venv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\Habib\Documents\django\FIVERR\venv\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Habib\Documents\django\FIVERR\venv\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "C:\Users\Habib\Documents\django\FIVERR\adam_mailk\dashboard\views.py", line 118, in UpdateReservation
return render(request, "dashboard/super/admin/update_reserve.html", context)
File "C:\Users\Habib\Documents\django\FIVERR\venv\lib\site-packages\django\shortcuts.py", line 19, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "C:\Users\Habib\Documents\django\FIVERR\venv\lib\site-packages\django\template\loader.py", line 62, in render_to_string
return template.render(context, request)
File "C:\Users\Habib\Documents\django\FIVERR\venv\lib\site-packages\django\template\backends\django.py", line 61, in render
return self.template.render(context)
File "C:\Users\Habib\Documents\django\FIVERR\venv\lib\site-packages\django\template\base.py", line 170, in render
return self._render(context)
File "C:\Users\Habib\Documents\django\FIVERR\venv\lib\site-packages\django\template\base.py", line 162, in _render
return self.nodelist.render(context)
File "C:\Users\Habib\Documents\django\FIVERR\venv\lib\site-packages\django\template\base.py", line 938, in render
bit = node.render_annotated(context)
File "C:\Users\Habib\Documents\django\FIVERR\venv\lib\site-packages\django\template\base.py", line 905, in render_annotated
return self.render(context)
File "C:\Users\Habib\Documents\django\FIVERR\venv\lib\site-packages\django\template\loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "C:\Users\Habib\Documents\django\FIVERR\venv\lib\site-packages\django\template\base.py", line 162, in _render
return self.nodelist.render(context)
File "C:\Users\Habib\Documents\django\FIVERR\venv\lib\site-packages\django\template\base.py", line 938, in render
bit = node.render_annotated(context)
File "C:\Users\Habib\Documents\django\FIVERR\venv\lib\site-packages\django\template\base.py", line 905, in render_annotated
return self.render(context)
File "C:\Users\Habib\Documents\django\FIVERR\venv\lib\site-packages\django\template\defaulttags.py", line 446, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "C:\Users\Habib\Documents\django\FIVERR\venv\lib\site-packages\django\urls\base.py", line 87, in reverse
return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "C:\Users\Habib\Documents\django\FIVERR\venv\lib\site-packages\django\urls\resolvers.py", line 685, in _reverse_with_prefix
raise NoReverseMatch(msg)
Exception Type: NoReverseMatch at /update-reservation/1/
Exception Value: Reverse for 'updatebar' with keyword arguments '{'updatebar': ''}' not found. 1 pattern(s) tried: ['updatebar/(?P<pk>[^/]+)/$']
views.py
def UpdateUserBar(request, pk):
user = request.user.id
user_email = request.user.email
bar = Bar.objects.filter(user_id=user)
form = UpdateBar(instance=bar)
if request.method == 'POST':
form = UpdateBar(request.POST, instance=bar)
if form.is_valid():
form.save()
return redirect('/updatebar')
messages.success(request, 'Bar Information Updated successfully')
else:
return redirect('/dashboard')
messages.error(request, 'Only Post method is accepted')
context = {"form":form, "user_email":user_email}
return render(request, "dashboard/super/landlord/create_bar.html", context)
urls.py
from django.urls import path
from .views import *
urlpatterns = [
path('updatebar/<str:pk>/', UpdateUserBar, name="updatebar"),
]
template.html
<li {% if request.path == '/updatebar/' %}class="active"{% endif %}><a href="{% url 'updatebar' updatebar=updatebar.pk %}"><i class="ion-plus-round"></i> <span class="nav-label">Create Table</span></a>
</li>
As defined in your urls.py, "update_bar" requires a parameter, in this case <str:pk>. You need to rewrite your redirect function in views.py to include this parameter, for example:
return redirect('updatebar', pk=pk)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.