'View on site' link gives error

Hi, I’m following the MDN Django tutorial and noticed an issue. I’m using Django 4.1.5 with Python 3.10.6. Per that tutorial, I added a ‘get_absolute_url’ method to the Book model like this:

class Book:
    ...
    def get_absolute_url(self):
        """Returns the URL to access a detail record for thid book"""
        return reverse('book-detail', args=[str(self.id)])

Doing this caused “View on Site” button to appear next to “History” on the Admin Book edit page as expected. When I hover over the button, I see the url: http://127.0.0.1:8000/admin/r/10/2/ If I click the button, I get the following error:

NoReverseMatch at /admin/r/10/2/

Reverse for 'book-detail' not found. 'book-detail' is not a valid view function or pattern name.

Request Method: 	GET
Request URL: 	http://127.0.0.1:8000/admin/r/10/2/
Django Version: 	4.1.5
Exception Type: 	NoReverseMatch
Exception Value: 	

Reverse for 'book-detail' not found. 'book-detail' is not a valid view function or pattern name.

Exception Location: 	/home/dow/django/venv1/lib/python3.10/site-packages/django/urls/resolvers.py, line 828, in _reverse_with_prefix
Raised during: 	django.contrib.contenttypes.views.shortcut
Python Executable: 	/home/dow/django/venv1/bin/python3
Python Version: 	3.10.6
Python Path: 	

['/home/dow/django/locallibrary',
 '/home/dow/ngsuite/ngsolve-install/lib/python3/dist-packages',
 '/home/dow/django',
 '/home/dow/django/locallibrary',
 '/usr/lib/python310.zip',
 '/usr/lib/python3.10',
 '/usr/lib/python3.10/lib-dynload',
 '/home/dow/django/venv1/lib/python3.10/site-packages']

Server time: 	Fri, 20 Jan 2023 08:54:40 -0800

I haven’t made any customizations to the admin templates. Also, I tried changing the function to use kwargs and ‘pk’ as in the Django documentation:

    def get_absolute_url(self):
        """Returns the URL to access a detail record for thid book"""
        return reverse('book-detail', kwargs={'pk' : self.pk})

but that gave the same error. Thanks for any help on this, and please let me know if a full stack trace would help.

We would need to see your urls.py file. Also identify whether that urls.py file is your root urls.py file or a file in an application being referenced by your root urls.py file. (The “root urls.py” file is the file identified in your settings.py file.)

Thanks, Ken! Here are the contents of the root urls.py file:

from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import include, path
from django.views.generic import RedirectView

# just for development, serve static files.
urlpatterns = [
    path('admin/', admin.site.urls),
    path('catalog/', include('catalog.urls')),
    path('', RedirectView.as_view(url='catalog/', permanent=True)),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

The application urls.py is currently empty, since this tutorial begins with building the model and playing with the admin pages.

That’s the cause of your error - you’re looking for a url named ‘book-detail’, but you don’t have that url defined yet.

1 Like

Ok – thanks! I assumed that generating the book-detail url and corresponding view/template was a function of the admin application. I probably just didn’t read far enough ahead in the tutorial…