NoReverseMatch at /cars Reverse for 'car_details' with keyword arguments '{'pk': ''}' not found. 1 pattern(s) tried: ['carscars/(?P<id>[0-9]+)/
stackoverflow
*
`
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>/car_details', views.car_details, name='car_details'),
]
In car.html
{% for car in cars %}
<div class="detail">
<h1 class="title">
<a href="{% url 'car_details' pk=car.id %}">{{car.car_title}}</a>
</h1>
<div class="location">
<a href="{% url 'car_details' pk=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
There is no pk
keyword argument in your route, as the error suggests.
You try to assign pk
here:
<a href="{% url 'car_details' pk=car.id %}">{{car.car_title}}</a>
But that'll throw an error because your route has one keyword argument id
and NOT pk
:
path('<int:id>/car_details', views.car_details, name='car_details'),
Change it to this:
<a href="{% url 'car_details' car.id %}">{{car.car_title}}</a>
There is no pk
keyword argument in your route, as the error suggests.
You try to assign pk
here:
<a href="{% url 'car_details' pk=car.id %}">{{car.car_title}}</a>
But that'll throw an error because your route has one keyword argument id
and NOT pk
:
path('<int:id>/car_details', views.car_details, name='car_details'),
Change it to this:
<a href="{% url 'car_details' car.id %}">{{car.car_title}}</a>
Also show same error by doing this stuff
Also show same error by doing this stuff