NoReverseMatch at / polls /(django教程)

时间:2015-10-15 14:54:53

标签: python django

我已经在SO上看到了其他一些问题,但他们并没有解决我的问题。我正在运行Django 1.8.5,Python 2.7.6。

我将根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/', include(admin.site.urls)),
]

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

我收到错误

NoReverseMatch at /polls/
u'polls' is not a registered namespace. 

我已尝试按照我建议的其他答案重新启动开发服务器,并在模板代码中包含{%load url from future%};两者都没有奏效。不确定我缺少什么,我的代码与教程中的文件匹配。

编辑: 轮询/ urls.py

from django.conf.urls import url
from . import views

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

1 个答案:

答案 0 :(得分:2)

如果ROOT_URLCONF设置为mysite.urls,则urls.py应位于嵌套mysite目录中,即mysite/mysite/urls.py