我想要实现的是,每当用户单击产品时,产品的 id 就会显示在 url 中。所以我尝试输入 <int:pk>
在网址中,但它说,
Page Not Found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/book-details/
homepage.html
{% for book in random %}
<a type="submit" href="{{% url 'book-details' %}}">
<img src="{{book.cover.url}}" height="300px" width="200px">
</a>
{% endfor %}
views.py
def book_details(request,pk):
return render(request, 'book-details.html')
urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', homepage, name='home'),
path('fiction', fiction, name='fiction'),
path('nonfiction', nonfiction, name='nonfiction'),
path('book-details/<int:pk>/', book_details, name='book-details'),
path('search-result', search_result, name='search-result')
最佳答案
您没有通过<int:pk>
调用 View 时,在 homepage.html 中进行以下更改:添加此代码
<a href="{% url 'book-details' book.pk %}"
关于python - 每当我尝试添加 <int :pk> in urls. py 时,在 Django 中找不到页面 (404),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65527305/