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.