django 找不到参数为“(',)”的“car_details”的反转,尝试了1个模式:['车厢/(?P< id>[0-9]+)/车厢详细信息\\Z']

jbose2ul  于 2023-01-21  发布在  Go
关注(0)|答案(2)|浏览(100)

堆栈溢出*

`
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
wqsoz72f

wqsoz72f1#

您的路由中没有pk关键字参数,正如错误所示。
您可以尝试在此处指定pk

<a href="{% url 'car_details' pk=car.id %}">{{car.car_title}}</a>

但这会抛出一个错误,因为您的路由有一个关键字参数id和NOT pk

path('<int:id>/car_details', views.car_details, name='car_details'),

将其更改为:

<a href="{% url 'car_details' car.id %}">{{car.car_title}}</a>
mzsu5hc0

mzsu5hc02#

执行此操作也会显示相同的错误

相关问题