问题
Trying to make django-registration work within the Django Tutorial polls projects.
I'm using Django 1.6, django-registration 1.0 and the django-registration-templates
When I try to access
http://localhost:8000/accounts/login/
I get
NoReverseMatch at /accounts/login/
Reverse for 'index' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
The line in the template, mysite/templates/base.html, that's cited in the error report is :
<a href="{% url 'index' %}">{% trans "Home" %}</a> |
And I do have a url with name 'index' in my polls.url :
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'),
)
So I feel like that should work ? Help ?
EDIT 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 *
回答1:
OK I've figured out the problem here. The url with name 'index' was defined in polls.urls and so within the template I needed to change :
<a href="{% url 'index' %}">{% trans "Home" %}</a>
to
<a href="{% url 'polls:index' %}">{% trans "Home" %}</a>
As mentioned in the Django 1.6 documention of the url tag and which I quote here for convenience :
If you’d like to retrieve a namespaced URL, specify the fully qualified name:
{% url 'myapp:view-name' %}
This will follow the normal namespaced URL resolution strategy, including using any hints provided by the context as to the current application.
来源:https://stackoverflow.com/questions/21619774/django-registration-noreversematch-at-accounts-login-at-accounts-login