2

我正在遵循 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')),


)

有任何想法吗?

4

2 回答 2

3

您需要的信息在您提供的回溯中,

在模板 /Users/Studio/Desktop/pod/t1/tut/tut/templates/base.html 中,第 16 行的错误为“索引”反向,参数“()”和关键字参数“{}”未找到。

在您的base.html模板中,在第 16 行,您使用 url 标签链接到名为“index”的视图。

16      <a href="{% url 'index' %}">{% trans "Home" %}</a> |

但是,您的 url 模式中没有命名indexurl 模式。因此反向失败,并引发NoReverseMatch异常。

您可以通过在模块中创建索引视图tut.views并将其添加到您的 url 模式来解决此问题。

url(r'^$', 'tut.views.index', name='index'),

或者,您可以{% url 'index' %}从模板中删除 ,直到添加索引视图。

于 2013-08-13T00:20:18.537 回答
1

添加 url(r'^', include('django.contrib.auth.urls'))到 url 模式

于 2020-08-29T13:34:56.383 回答