堆栈溢出*
`
In views.py
def car_details(request, id):
single_car = get_object_or_404(Car, pk=id),
# id = 1
data = {
'single_car': single_car,
# 'url': url,
}
# reverse('single_car', args=(id))
return render(request, 'cars/car_details.html', data)
In urls.py
urlpatterns = [
path('', views.cars, name='cars'),
path('<int:id>', views.car_details, name='car_details'),
]
In car.html
{% for car in cars %}
<div class="detail">
<h1 class="title">
<a href="{% url 'car_details' car.id %}">{{car.car_title}}</a>
</h1>
<div class="location">
<a href="{% url 'car_details' car.id %}">
<i class="flaticon-pin"></i>{{car.state}}, {{car.city}}
</a>
</div>
{% endfor %}
In models.py
class Car(models.Model):
id=models.IntegerField(primary_key=True,default=True)
also tried with revrser function on views.py it gives same error
``*`
also tried with revrsere function on views.py it gives same error
2条答案
按热度按时间wqsoz72f1#
您的路由中没有
pk
关键字参数,正如错误所示。您可以尝试在此处指定
pk
:但这会抛出一个错误,因为您的路由有一个关键字参数
id
和NOTpk
:将其更改为:
mzsu5hc02#
执行此操作也会显示相同的错误