NoReverseMatch django-不是有效的视图函数或模式 [英] NoReverseMatch django - not a valid view function or pattern
问题描述
当前使用Django 1.11.我有
Currently using Django 1.11. I get an exception of
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
我想在详细信息页面中使用我的模型中的 get_absolute_url
转到更新"页面.当我取出对.id的引用并使用get_absolute_url时.我检查了一下是否正确引用了名称"book_details".我可以转到页面,使书籍详细信息正确呈现.在Django的管理控制台中,查看网站"按钮也无法正确呈现,它显示了该本地主机:8000/admin/r/13/c7311ecf-eba7-4e9d-8b1a-8ba4e075245a/,因此无法获取库/要么是书
I want to use the get_absolute_url
from my Model in the details page to go to a Update page. When I take out the reference to the .id and use the get_absolute_url. I checked to see the name "book_details" is referenced properly. I can go to the page and have books details render properly. In the admin console of Django, the "view on site" button also does not render properly it shows this localhost:8000/admin/r/13/c7311ecf-eba7-4e9d-8b1a-8ba4e075245a/ so it does not get the library/books either
当前< a href ="{{book.id}}/update">更新</a>
期望< a href ="{{book.get_absolute_url}}/update"> Update</a>
我在哪里输错了以使其不起作用?
Where have I mistyped for this to not work?
文件设置:
是的,我确实将UUID作为主键.
Yes, I do have UUID as the primary key.
在 views.py
class BookDetailsView(generic.DetailView):
"""
Generic class-based detail view for a Book.
"""
model = Book
在 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'),
在 models.py
Class Book(models.Model)
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)])
推荐答案
尝试将 pk
作为关键字参数提供给 reverse
函数
Try providing the pk
as keyword argument to the reverse
function,
def get_absolute_url(self):
return reverse('book_details', kwargs={ 'pk': str(self.id) })
此外,您在网址末尾缺少斜杠,
Also, you are missing a 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'),
这篇关于NoReverseMatch django-不是有效的视图函数或模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!