简体   繁体   English

映射项目时 Django 中的 NoReverseMatch 错误

[英]NoReverseMatch Error in Django when mapping items

I am trying to map different items using the pk(primary key).我正在尝试使用 pk(主键) map 不同的项目。 I have used it in my urls.py, views.py as well as the templates file.我已经在我的 urls.py、views.py 以及模板文件中使用了它。 But I am a NoReverseMatch Error.但我是 NoReverseMatch 错误。 I have also tried changing the str to int in the urls.py file but it still gave the same error.我也尝试在 urls.py 文件中将 str 更改为 int ,但它仍然给出了相同的错误。 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视图.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网址.py

from django.urls import path
from .views import *

urlpatterns = [
    path('updatebar/<str:pk>/', UpdateUserBar, name="updatebar"),
]

template.html模板.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>.正如您在 urls.py 中定义的那样,“update_bar”需要一个参数,在本例中为 <str:pk>。 You need to rewrite your redirect function in views.py to include this parameter, for example:您需要在 views.py 中重写您的重定向 function 以包含此参数,例如:

return redirect('updatebar', pk=pk)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM