NoReverseMatch at / answer /反向'详细',找不到关键字参数'{'pk':1}'。尝试了1种模式:['answer / $(?P <pk> \\ d +)/ $']

时间:2017-11-23 07:00:11

标签: python django

由于某些原因,我似乎无法解决这个错误。 ,任何帮助将不胜感激

这是 的 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

1 个答案:

答案 0 :(得分:0)

由于您要包含答案中的网址,因此您需要删除$符号,这表示正则表达式已结束..

url(r'^answer/$',include('qna.urls', namespace='qna')),
             /\

应该是

url(r'^answer/',include('qna.urls', namespace='qna')),
相关问题