我正在关注Django注册的入门指南,当我访问http://localhost:8080/accounts/register/
时出现以下错误:
NoReverseMatch at /accounts/register/
Reverse for 'index' with arguments '()' and keyword arguments '{}' not found.
Request Method: GET
Request URL: http://localhost:8080/accounts/register/
Django Version: 1.5.1
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'index' with arguments '()' and keyword arguments '{}' not found.
Exception Location: /Library/Python/2.7/site-packages/django/template/defaulttags.py in render, line 424
Python Executable: /usr/bin/python
Python Version: 2.7.1
Python Path:
['/Users/Studio/Desktop/orro/t1/tut',
'/Library/Python/2.7/site-packages/setuptools-0.9.8-py2.7.egg',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC',
'/Library/Python/2.7/site-packages']
Server time: Mon, 12 Aug 2013 18:53:54 -0500
Error during template rendering
In template /Users/Studio/Desktop/pod/t1/tut/tut/templates/base.html, error at line 16
Reverse for 'index' with arguments '()' and keyword arguments '{}' not found.
6 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
7
8 <head>
9 <link rel="stylesheet" href="/style.css" />
10 <title>{% block title %}User test{% endblock %}</title>
11 </head>
12
13 <body>
14 <div id="header">
15 {% block header %}
16 <a href="{% url 'index' %}">{% trans "Home" %}</a> |
17
18 {% if user.is_authenticated %}
19 {% trans "Logged in" %}: {{ user.username }}
20 (<a href="{% url 'auth_logout' %}">{% trans "Log out" %}</a> |
21 <a href="{% url 'auth_password_change' %}">{% trans "Change password" %}</a>)
22 {% else %}
23 <a href="{% url 'auth_login' %}">{% trans "Log in" %}</a>
24 {% endif %}
25 <hr />
26 {% endblock %}
我的urls.py:
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'tut.views.home', name='home'),
# url(r'^tut/', include('tut.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
(r'^accounts/', include('registration.backends.default.urls', namespace='registration')),
)
有什么想法吗?
答案 0 :(得分:3)
您需要的信息在您提供的追溯中,
在模板/Users/Studio/Desktop/pod/t1/tut/tut/templates/base.html中,第16行出错 使用参数'()'和未找到关键字参数'{}来反转'index'。
在第{16}行的base.html
模板中,您使用url标记链接到名为“index”的视图。
16 <a href="{% url 'index' %}">{% trans "Home" %}</a> |
但是,您的网址格式中没有名为index
的网址格式。因此反向失败,并引发NoReverseMatch
异常。
您可以通过在tut.views
模块中创建索引视图并将其添加到网址模式来解决此问题。
url(r'^$', 'tut.views.index', name='index'),
或者,您可以从模板中删除{% url 'index' %}
,直到添加索引视图。
答案 1 :(得分:1)
将url(r'^', include('django.contrib.auth.urls'))
添加到网址格式