Search code examples
pythondjangohttp-status-code-404

Page not found (404) in Django, whenever I tried adding <int:pk> in urls.py


What I wanted to achieve is for the product's id to be shown in the url, whenever a user clicks it. So I tried putting <int:pk> in the url, but it says,

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')

Solution

  • You are not passing <int:pk> when calling the view , make the following change in homepage.html : add this code

    <a href="{% url 'book-details' book.pk %}"