Django, NoReverseMatch. Что я должен написать в этом коде? - PullRequest
0 голосов
/ 23 апреля 2020

У меня проблема:

NoReverseMatch at /update-orders/12
Reverse for 'update_order' with arguments '('',)' not found. 1 pattern(s) tried: ['update\\-orders/(?P<pk>[0-9]+)$']
Request Method: GET
Request URL:    http://192.168.0.249:8000/update-orders/12
Django Version: 3.0.5
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'update_order' with arguments '('',)' not found. 1 pattern(s) tried: ['update\\-orders/(?P<pk>[0-9]+)$']
Exception Location: /root/.local/share/virtualenvs/myp4-4l8n6HJk/lib/python3.7/site-packages/django/urls/resolvers.py in _reverse_with_prefix, line 677
Python Executable:  /root/.local/share/virtualenvs/myp4-4l8n6HJk/bin/python
Python Version: 3.7.3
Python Path:    
['/var/workspace/myp4/webprint',
 '/usr/lib/python37.zip',
 '/usr/lib/python3.7',
 '/usr/lib/python3.7/lib-dynload',
 '/root/.local/share/virtualenvs/myp4-4l8n6HJk/lib/python3.7/site-packages']

Почему у меня возникает эта ошибка во время рендеринга шаблона?

detail_order. html

{% extends 'index.html' %}

{% block content %}
<p><a href="{% url 'orders' %}">Back</a></p>
<h1>Заказ {{get_order.number_order}}</h1>
<h2><a href="{% url 'update_order' get_order.id %}">Update</a></h2>

views.py

class UpdateOrderView(CustomSuccessMessageMixin, UpdateView):
    model = Order
    template_name = 'detail_order.html'
    form_class = OrderForm
    success_url = reverse_lazy('detail_order')
    success_msg = 'Ok'
    def get_context_data(self, **kwargs):
        kwargs['update'] = True
        return super().get_context_data(**kwargs)

urls.py

from django.contrib import admin
from django.urls import path, include
from .views import *
from print import views

urlpatterns = [
    path('', views.home_page, name='index'),
    path('orders', views.OrderCreateView.as_view(), name='orders'),
    path('update-orders/<int:pk>', views.UpdateOrderView.as_view(), name='update_order'), # THIS PATH
    path('delete-orders/<int:pk>', views.DeleteOrderView.as_view(), name='delete_order'),
    path('detail-order/<int:pk>', views.DetailOrderView.as_view(), name='detail_order'),
    path('login', views.MyprojectLoginView.as_view(), name='login_page'),
    path('register', views.RegisterUserView.as_view(), name='register_page'),
    path('logout', views.MyprojectLogout.as_view(), name='logout_page'),
]

1 Ответ

0 голосов
/ 23 апреля 2020

В шаблоне укажите order.id вместо get_order.id. Тогда ваш код шаблона будет:

<h2><a href="{% url 'update_order' order.id %}">Update</a></h2>
...