我一直关注Django tutorial part 3,当我尝试查看http://localhost:8000/polls/时出现以下错误:
**Reverse for 'detail' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'polls/(?P<question_id>[0-9]+)/$']**
我的文件如下:
的mysite / urls.py:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls', namespace="polls")),
url(r'^admin/', admin.site.urls),
]
轮询/ urls.py:
from django.conf.urls import url
from . import views
app_name = 'polls'
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'),
url(r'^(?P<question_id>[0-9]+)/results/$', views.results, name='results'),
url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
]
轮询/ detail.html:
<h1>{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
<li>{{ choice.choice_text }}</li>
{% endfor %}
</ul>
轮询/模板/轮询/ index.html中:
<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
这个错误是什么意思?
如何调试?
你能建议修复吗?
N.b。我已经看到并尝试了类似问题的答案:
NoReverseMatch at /polls/ (django tutorial) Django 1.8.2 -- Tutorial Chapter 3 -- Error: NoReverseMatch at /polls/ -- Python 3.4.3 NoReverseMatch - Django 1.7 Beginners tutorial Django: Reverse for 'detail' with arguments '('',)' and keyword arguments '{}' not found https://groups.google.com/forum/#!msg/django-users/etSR78dgKBo/euSYcSyMCgAJ NoReverseMatch at /polls/ (django tutorial) https://www.reddit.com/r/django/comments/3d43gb/noreversematch_at_polls1results_in_django/
编辑,我最初错过了以下问题。它的优秀答案部分回答了我的问题(如何调试),但没有涵盖我的具体问题。
答案 0 :(得分:2)
这就是问题:
polls / templates / polls / index.html应该是:
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
我无意中用以下行替换了整个文件,而不仅仅是更新了相关的行(隐含here):
<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
如@Sayse在评论中所述,这意味着question.id为空,导致错误。