Django教程第3部分 - 在/ polls /的NoReverseMatch

时间:2022-10-30 18:07:22

I have been following the Django tutorial part 3 and am getting the following error when I attempt to view http://localhost:8000/polls/:

我一直在关注Django教程第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]+)/$']**

My files are as follows:

我的文件如下:

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),
]

polls/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'),
]

polls/detail.html:

<h1>{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
    <li>{{ choice.choice_text }}</li>
{% endfor %}
</ul>

polls/templates/polls/index.html:

<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>

What does this error mean?

这个错误是什么意思?

How do I debug it?

我该如何调试呢?

Can you suggest a fix?

你能建议解决吗?

N.b. I have seen and tried the answers to the similar questions:

注:我已经看到并尝试过类似问题的答案:

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/

NoReverseMatch at / polls /(django tutorial)Django 1.8.2 - 教程第3章 - 错误:NoReverseMatch at / polls / - Python 3.4.3 NoReverseMatch - Django 1.7初学者教程Django:反向'详细'带参数'( '',')'和关键字参数'{}'未找到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/

Edit, I initially missed the following question. Its excellent answer partially answers my question (how to debug) but does not cover my specific problem.

编辑,我最初错过了以下问题。它的优秀答案部分回答了我的问题(如何调试),但没有涵盖我的具体问题。

What is a NoReverseMatch error, and how do I fix it?

什么是NoReverseMatch错误,我该如何解决?

1 个解决方案

#1


1  

This was the problem:

这就是问题所在:

polls/templates/polls/index.html should have been:

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 %}

I had inadvertantly replaced the entire file with the following line, rather than just updating the relevant line (implied here):

我无意中用以下行替换了整个文件,而不是只更新相关的行(这里暗示):

<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>

As stated by @Sayse in the comments, this would mean that question.id is empty resulting in the error.

正如@Sayse在评论中所说,这意味着question.id为空,导致错误。

#1


1  

This was the problem:

这就是问题所在:

polls/templates/polls/index.html should have been:

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 %}

I had inadvertantly replaced the entire file with the following line, rather than just updating the relevant line (implied here):

我无意中用以下行替换了整个文件,而不是只更新相关的行(这里暗示):

<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>

As stated by @Sayse in the comments, this would mean that question.id is empty resulting in the error.

正如@Sayse在评论中所说,这意味着question.id为空,导致错误。