簡體   English   中英

映射項目時 Django 中的 NoReverseMatch 錯誤

[英]NoReverseMatch Error in Django when mapping items

我正在嘗試使用 pk(主鍵) map 不同的項目。 我已經在我的 urls.py、views.py 以及模板文件中使用了它。 但我是 NoReverseMatch 錯誤。 我也嘗試在 urls.py 文件中將 str 更改為 int ,但它仍然給出了相同的錯誤。 我希望我能得到一個好心的幫助。 謝謝

錯誤日志

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>[^/]+)/$']

視圖.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)

網址.py

from django.urls import path
from .views import *

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

模板.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>

正如您在 urls.py 中定義的那樣,“update_bar”需要一個參數,在本例中為 <str:pk>。 您需要在 views.py 中重寫您的重定向 function 以包含此參數,例如:

return redirect('updatebar', pk=pk)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM