尝试在Django Tutorial投票项目中进行django-registration工作。
我正在使用Django 1.6,django-registration 1.0和django-registration-templates
当我尝试访问
时http://localhost:8000/accounts/login/
我得到了
NoReverseMatch at /accounts/login/
Reverse for 'index' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
错误报告中引用的模板mysite / templates / base.html中的行是:
<a href="{% url 'index' %}">{% trans "Home" %}</a> |
我的民意调查中有一个名为'index'的网址:
urlpatterns = patterns('',
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
url(r'^(?P<pk>\d+)/results/$', views.ResultsView.as_view(), name='results'),
url(r'^(?P<poll_id>\d+)/vote/$', views.vote, name='vote'),
)
所以我觉得这应该有用吗?帮忙?
编辑1
polls.urls:
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('',
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
url(r'^(?P<pk>\d+)/results/$', views.ResultsView.as_view(), name='results'),
url(r'^(?P<poll_id>\d+)/vote/$', views.vote, name='vote'),
)
mysite.urls:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls', namespace="polls")),
url(r'^admin/', include(admin.site.urls)),
url(r'^accounts/', include('registration.backends.default.urls')),
)
registration.urls:
"""
Backwards-compatible URLconf for existing django-registration
installs; this allows the standard ``include('registration.urls')`` to
continue working, but that usage is deprecated and will be removed for
django-registration 1.0. For new installs, use
``include('registration.backends.default.urls')``.
"""
import warnings
warnings.warn("include('registration.urls') is deprecated; use include('registration.backends.default.urls') instead.",
DeprecationWarning)
from registration.backends.default.urls import *
答案 0 :(得分:4)
好的,我已经在这里找到了问题。名为'index'的网址在polls.urls中定义,因此我需要更改模板中的内容:
<a href="{% url 'index' %}">{% trans "Home" %}</a>
到
<a href="{% url 'polls:index' %}">{% trans "Home" %}</a>
如Django 1.6 documention of the url tag中所述,为方便起见,我在此引用:
如果您要检索命名空间的URL,请完全指定 合格名称:
{%url'myapp:view-name'%}
这将遵循正常的命名空间URL解析策略, 包括使用上下文提供的关于当前的任何提示 应用