Reverse for 'book_detail_view' not found. 'book_detail_view' is not a valid view function or pattern name.

{% extends “base_generic.html” %}

{% block content %}

 <h1>Book List</h1>
  {% if book_list %}
   <ul>
    {% for book in book_list %}
     <li>
     <a href="{% url 'book_detail_view' book %}">{{ book.title }}</a> ({{book.author}})
     </li>
    {% endfor %}
   </ul>
   {% else %}
      <p>There are no books in the library.</p>
  {% endif %}
{% endblock %}

def book_detail_view(request, primary_key):
try:
book = Book.objects.get(pk=primary_key)
except Book.DoesNotExist:
raise Http404(‘Book does not exist’)

return render(request, 'catalog/book_detail.html', context={'book': book})

here you should use book.id instead of book like

<a href="{% url 'book_detail_view' book.id %}

also you need to create url for this particular book_detail_view if you are trying to directly access the view function then it won’t work this way

Hi, thanks for your help. There is same error. Can you propose me an example of what I suppose to do to solve this issue?

What do your urls.py files look like?

url.py →
from django.urls import path
from . import views

urlpatterns = [
path(‘’, views.index, name=‘index’),
path(‘books/’, views.BookListView.as_view(), name=‘books’),
path(‘book/int:pk’, views.Book_detail_view.as_view(), name=‘book-detail’),
]

views.py →
from django.shortcuts import render
from .models import Book, Author, BookInstance, Genre
from django.views import generic
from django.shortcuts import get_object_or_404

Create your views here.

def index(request):
“”“View function for home page of site.”“”

# Generate counts of some of the main objects
num_books = Book.objects.all().count()
num_instances = BookInstance.objects.all().count()

# Available books (status = 'a')
num_instances_available = BookInstance.objects.filter(status__exact='a').count()

# The 'all()' is implied by default.
num_authors = Author.objects.count()

context = {
    'num_books': num_books,
    'num_instances': num_instances,
    'num_instances_available': num_instances_available,
    'num_authors': num_authors,
    }

# Render the HTML template index.html with the data in the context variable
return render(request, 'index.html', context=context)

class BookListView(generic.ListView):
“”“Generic class-based view for a list of books.”“”
model = Book
paginate_by = 10

def book_detail_view(request, primary_key):
try:
book = Book.objects.get(pk=primary_key)
except Book.DoesNotExist:
raise Http404(‘Book does not exist’)

return render(request, 'catalog/book_detail.html', context={'book': book})

So the name of the url is book-detail, not book_detail_view - that’s what you need to use in the url tag.

Request Method: GET
Request URL: http://127.0.0.1:8000/catalog/books/2

Now, I can’t link the page book_detail.html
I now have this:

Request Method: GET
Request URL: http://127.0.0.1:8000/catalog/books/2

I don’t understand what you’re trying to comment on.

Please post the change that you made.

Also, when posting code, please remember to surround the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```.

Here is the URL.py updated

url.py
from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('books/', views.BookListView.as_view(), name='books'),
    path('book/<int:pk>', views.book_detail_view, name='book-detail'),
] 
views.py
def book_detail_view(request, primary_key):
    try:
        book = Book.objects.get(pk=primary_key)            
    except Book.DoesNotExist:
        raise Http404('Book does not exist')
    
    return render(request, 'catalog/book_detail.html', context={'book': book})
book_list.html
{% for book in book_list %}
     <li>
     <a href="{% url 'book-detail' book.pk %}">{{ book.title }}</a> ({{book.author}})
     </li>
    {% endfor %}

This is the result:

TypeError at /catalog/book/5
book_detail_view() got an unexpected keyword argument 'pk'
Request Method:	GET
Request URL:	http://127.0.0.1:8000/catalog/book/5
Django Version:	4.1.2
Exception Type:	TypeError
Exception Value:	
book_detail_view() got an unexpected keyword argument 'pk'
Exception Location:	C:\Users\Marie Robert\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py, line 197, in _get_response

What is the problem now please?

The name of the parameter in the url does not match the name of the parameter in your view function definition.

Which parameters please?

Review the docs and examples at Writing your first Django app, part 3 | Django documentation | Django