我一直對這個錯誤了一個星期的大部分時間,這是我不斷收到錯誤:Django的無反向匹配錯誤
>
NoReverseMatch at /practice/practice/2/saleinfoedit/
Reverse for 'car_detail' with keyword arguments '{'pk': ''}' not found. 1 pattern(s) tried: ['practice/practice/(?P<pk>\\d)$']
我已經簡化了代碼我想這是什麼錯誤的相關部分。這個想法是有一個汽車列表頁面,當你點擊汽車鏈接,你可以編輯車輛的銷售歷史。最終我會爲這部分安裝套件,但是babysteps。下面是相關的代碼:
models.py
class Car(models.Model):
car_name = models.CharField(max_length=200)
color = models.CharField(max_length=200)
age = models.CharField(max_length=200)
def get_absolute_url(self):
return reverse('practice:car_detail',kwargs={'pk':self.pk})
def __str__(self):
return '%s' %(self.car_name)
class SaleInfo(models.Model):
car_name = models.ForeignKey(Car, on_delete=models.CASCADE,)
price = models.CharField(max_length=100)
date = models.CharField(max_length=100)
comments = models.CharField(max_length=200)
def __str__(self):
return '%s' %(self.car_name)
def get_absolute_url(self):
return reverse('practice:car_detail',kwargs={'pk':self.pk})
views.py
class IndexView(generic.ListView):
template_name = 'practice/carlist.html'
context_object_name = 'latest_car_list'
def get_queryset(self):
return Car.objects.all()
class DetailView(generic.DetailView):
model = Car
form_class = CarForm
template_name = 'practice/car_detail.html'
class UpdateView(generic.UpdateView):
model = Car
form_class = CarFormEdit
class SaleInfoUpdateView(generic.UpdateView):
model = SaleInfo
form_class = SaleInfoFormEdit
template_name = 'practice/saleinfo_form.html'
urls.py
app_name = 'practice'
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'), # shows lists of Cars via Car Name
url(r'^practice/(?P<pk>\d)$', views.DetailView.as_view(), name='car_detail'),
url(r'^practice/(?P<pk>\d)/edit/$', views.UpdateView.as_view(), name='car_edit'),
url(r'^practice/(?P<pk>\d)/saleinfoedit/$', views.SaleInfoUpdateView.as_view(), name='saleinfo_edit'),
]
car_detail.html
{% extends 'practice/car_base.html' %}
{% block post_content %}
<div class="col-md-8">
<a class='btn btn-primary' href="{% url 'practice:car_edit' pk=car.pk %}">
<span class='glyphicon glyphicon-pencil'></span>
</a>
<p>{{ car.car_name|safe }}</p>
<p>{{ car.color|safe }} {{ car.age|safe }} </p>
<a class='btn btn-primary' href="{% url 'practice:saleinfo_edit' pk=car.pk %}">
<span class='glyphicon glyphicon-pencil'> SaleInfo</span>
</a>
</div>
{% endblock %}
saleinfo_form.html
{% extends 'practice/car_base.html' %}
{% block post_content %}
{% load bootstrap3 %}
<form class="" action="" method="post">
{% csrf_token %}
{% bootstrap_form form %}
<!-- {{form.as_p}} -->
<input type="submit" name="" value="Update">
</form>
{% endblock %}
我可以在必要時發佈我的模板了。我確信這是簡單的,我一直在尋找過去,但幾天後我迷路了。
安置自己的模板,你在哪裏上市了被點擊這些網址。 – marcusshep
嗨,這篇文章可以幫助你[示例](https://stackoverflow.com/a/17824769/7261317)其必須工作... – Robert
我已經在模板中添加了請求。 – Jcb1125