NoReverseMatch django is not a valid view function or template
Currently using Django 1.11. I am getting an exception from
Reverse for 'book_details' not found. 'book_details' is not a valid view function or pattern name.
Request Method: GET
Request URL: http://localhost:8000/library/book/c7311ecf-eba7-4e9d-8b1a-8ba4e075245a/
Django Version: 1.11
Exception Type: NoReverseMatch
I want to use get_absolute_url
from my model details page to go to the update page. When I take a link to the .id and use get_absolute_url. I have verified that the name "book_details" is correct. I can go to the page and set up the book data correctly. In the Django admin console, the "view on site" button also doesn't display correctly, it shows this localhost: 8000 / admin / r / 13 / c7311ecf-eba7-4e9d-8b1a-8ba4e075245a / so it doesn't get the library / books either
current
<a href =" {{ book.id }}/update">Update</a>
desired
<a href =" {{ book.get_absolute_url }}/update">Update</a>
Where did I go wrong to prevent this from working?
Setting in files:
Yes, I have UUID as primary key.
IN views.py
class BookDetailsView(generic.DetailView):
"""
Generic class-based detail view for a Book.
"""
model = Book
in urls.py
url(r'^book/(?P<pk>[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})$', views.BookDetailsView.as_view(), name='book_details'),
url(r'^book/(?P<pk>[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/update/$', views.BookUpdate.as_view(), name='book_update'),
in models.py
class Book (models.Model):
def get_absolute_url(self):
"""Returns the URL of the book for details"""
return reverse('book_details', args=[str(self.id)])
source to share
Try to provide pk
as a key argument to a function reverse
,
def get_absolute_url(self):
return reverse('book_details', kwargs={ 'pk': str(self.id) })
Also, you are missing the trailing slash at the end of the url,
url(r'^book/(?P<pk>[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/$', views.BookDetailsView.as_view(), name='book_details'),
source to share