这是 的 models.py
def get_absolute_url(self):
# return f"/question/{self.slug}"
return reverse('qna:detail', kwargs={'pk': self.pk})
QNA / Urls.py
url(r'^$', AnsListView.as_view(),name='list'),
url(r'^create$', AnsCreateView.as_view(),name='create'),
url(r'^(?P<pk>\d+)/$', AnsDetailView.as_view(),name='detail'),
主要/ url.py
url(r'^answer/$',include('qna.urls', namespace='qna')),
尝试加载http://127.0.0.1:8000/answer/ 时,错误
NoReverseMatch at /answer/
Reverse for 'detail' with keyword arguments '{'pk': 1}' not found. 1 pattern(s) tried: ['answer/$(?P<pk>\\d+)/$']
Request Method: GET
Request URL: http://127.0.0.1:8000/answer/
Django Version: 1.11.2
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'detail' with keyword arguments '{'pk': 1}' not found. 1 pattern(s) tried: ['answer/$(?P<pk>\\d+)/$']
Exception Location: C:\Users\prash\Desktop\prashantks\pksproject\lib\site-packages\django\urls\resolvers.py in _reverse_with_prefix, line 497
答案 0 :(得分:0)
由于您要包含答案中的网址,因此您需要删除$
符号,这表示正则表达式已结束..
url(r'^answer/$',include('qna.urls', namespace='qna')),
/\
应该是
url(r'^answer/',include('qna.urls', namespace='qna')),