NoReverseMatch at / vehicles / delete / 1 / Reverse for' detail'使用关键字参数' {' pk':''}'未找到

时间:2017-07-27 18:02:11

标签: django

请帮忙。我设置了一个应用程序。我的更新正在运行,但删除不是。它给出了:

NoReverseMatch at /vehicles/delete/1/

Reverse for 'detail' with keyword arguments '{'pk': ''}' not found. 1 pattern(s) tried: ['vehicles/(?P<pk>\\d+)/$']

这是我的代码:

models.py:

from django.db import models
from django.core.urlresolvers import reverse

class Vehicles(models.Model):

    vehicle_id = models.AutoField(primary_key=True)
    vin_number = models.CharField(unique=True, max_length=17, blank=True, null=True, error_messages={'unique':"VIN already exists"})
    make = models.CharField(max_length=15, blank=True, null=True)
    model = models.CharField(max_length=15, blank=True, null=True)
    year = models.IntegerField(blank=True, null=True)
    vehicle_type = models.CharField(max_length=8)

    class Meta:
        managed = True
        db_table = 'vehicles'

    def get_absolute_url(self):
        return reverse('vehicles:detail', kwargs={'pk': self.pk})

views.py:

from django.core.urlresolvers import reverse_lazy
from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView
from django.contrib.auth.mixins import LoginRequiredMixin
from . import models

class VehicleUpdateView(LoginRequiredMixin, UpdateView):
    fields = '__all__'
    model = models.Vehicles
    template_name = 'vehicles/vehicle_form.html'


class VehicleDeleteView(LoginRequiredMixin, DeleteView):
    model = models.Vehicles
    success_url = reverse_lazy('vehicles:all')
    template_name = 'vehicles/vehicle_confirm_delete.html'

urls.py:

urlpatterns = [
    url(r'^$', views.VehicleListView.as_view(), name='all'),
    url(r'^(?P<pk>\d+)/$', views.VehicleDetailView.as_view(), name='detail'),
    url(r'^create/$', views.VehicleCreateView.as_view(), name='create'),
    url(r'^update/(?P<pk>\d+)/$', views.VehicleUpdateView.as_view(), name='update'),
    url(r'^delete/(?P<pk>\d+)/$', views.VehicleDeleteView.as_view(), name='delete'),
]

vehicle_detail.html:

<h1>VIN: {{ vehicle_detail.vin_number }}</h1>
<p>id: {{ vehicle_detail.vehicle_id }}</p>
<!--<p>vehicle_detail.vin_number</p>-->
<p>make: {{vehicle_detail.make }}</p>
<p>model: {{ vehicle_detail.model }}</p>
<p>year: {{ vehicle_detail.year }}</p>
<p>type: {{ vehicle_detail.vehicle_type }}</p>

<p><a class="btn btn-warning" href="{% url 'vehicles:update' pk=vehicle_detail.vehicle_id %}">Update</a></p>
<p><a class="btn btn-danger" href="{% url 'vehicles:delete' pk=vehicle_detail.vehicle_id %}">Delete</a></p>
<p><a href="{% url 'vehicles:all' %}">back to vehicle list</a></p>

vehicle_confirme_delete.html:

 <h1>Delete: {{ vehicle.make }} {{ vehicle.vin_number }}</h1>
    <form method="POST">
        {% csrf_token %}
        <input type="submit" class="btn btn-danger" value="Delete">
        <a href="{% url 'vehicles:detail' pk=vehicle.pk %}">Cancel</a>
    </form>

2 个答案:

答案 0 :(得分:0)

模型为Vehicles,因此Django会将模板上下文中的实例包含为vehicles,而不是vehicle。将链接更改为:

<a href="{% url 'vehicles:detail' pk=vehicles.pk %}">Cancel</a>

同样,您应该在详细信息模板中使用vehicles而不是vehicle_detail

请注意,使用vehicles会有点混乱,因为它只是一辆车,而不是车辆列表。另一种选择是在您的视图中设置context_object_name。然后,您可以在模板中使用vehicle

class VehicleDeleteView(LoginRequiredMixin, DeleteView):
    model = models.Vehicles
    context_object_name = 'vehicle' 
    ... 

最佳选择可能是将模型重命名为Vehicle。这将遵循Django建议,为Django模型使用单数而不是复数,并允许您在模板中使用vehicle而不设置context_object_name

答案 1 :(得分:0)

我发现了一个错误。在vehicle_confirme_delete.html中应该是:

<a href="{% url 'vehicles:detail' pk=vehicle.pk %}">Cancel</a>

而不是:

{{1}}