django-registration-redux - NoReverseMatch at / accounts / register /

时间:2014-12-28 02:15:57

标签: python django django-urls

我跟随"Tango with Django" tutorial,其中一章是关于使用Registration View Redux来处理用户身份验证。除了我注册新用户时,一切正常。用户实际上添加得很好。但在提交之后,而不是被重定向到"注册完成"页面,我收到此错误:

NoReverseMatch at /accounts/register/

Reverse for 'registration' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Request Method: POST
Request URL:    http://192.168.2.10:5555/accounts/register/
Django Version: 1.7
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'registration' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Exception Location: C:\Python34\Lib\site-packages\django\core\urlresolvers.py in _reverse_with_prefix, line 463
Python Executable:  C:\Python34\python.exe
Python Version: 3.4.2
Python Path:    
['D:\\_python_projects\\tango_with_django_project',
 'C:\\Python34\\Lib\\site-packages\\setuptools-8.2.1-py3.4.egg',
 'C:\\Python34\\lib\\site-packages\\setuptools-8.2.1-py3.4.egg',
 'C:\\Python34\\Lib\\site-packages',
 'C:\\Windows\\system32\\python34.zip',
 'C:\\Python34\\DLLs',
 'C:\\Python34\\lib',
 'C:\\Python34']
Server time:    Sat, 27 Dec 2014 21:04:15 -0500

回溯:

Environment:


Request Method: POST
Request URL: http://192.168.2.10:5555/accounts/register/

Django Version: 1.7
Python Version: 3.4.2
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'rango',
 'registration')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "C:\Python34\Lib\site-packages\django\core\handlers\base.py" in get_response
  111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python34\Lib\site-packages\django\views\generic\base.py" in view
  69.             return self.dispatch(request, *args, **kwargs)
File "C:\Python34\Lib\site-packages\registration\views.py" in dispatch
  79.         return super(RegistrationView, self).dispatch(request, *args, **kwargs)
File "C:\Python34\Lib\site-packages\django\views\generic\base.py" in dispatch
  87.         return handler(request, *args, **kwargs)
File "C:\Python34\Lib\site-packages\registration\views.py" in post
  35.             return self.form_valid(request, form)
File "C:\Python34\Lib\site-packages\registration\views.py" in form_valid
  90.             return redirect(to, *args, **kwargs)
File "C:\Python34\Lib\site-packages\django\shortcuts.py" in redirect
  74.     return redirect_class(resolve_url(to, *args, **kwargs))
File "C:\Python34\Lib\site-packages\django\shortcuts.py" in resolve_url
  158.         return urlresolvers.reverse(to, args=args, kwargs=kwargs)
File "C:\Python34\Lib\site-packages\django\core\urlresolvers.py" in reverse
  546.     return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "C:\Python34\Lib\site-packages\django\core\urlresolvers.py" in _reverse_with_prefix
  463.                              (lookup_view_s, args, kwargs, len(patterns), patterns))

Exception Type: NoReverseMatch at /accounts/register/
Exception Value: Reverse for 'registration' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

我的注册表格模板:

{% extends "rango/base.html" %}
{% block title %}Register{% endblock %}

{% block body_block %}
    <h1>Register to Rango</h1>
    <form method="post" action=".">
        {% csrf_token %}
        {{ form.as_p }}

        <input type="submit" value="Submit" />
    </form>
{% endblock %}

urls.py

from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf import settings


urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'tango_with_django_project.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^admin/', include(admin.site.urls)),
    url(r'^accounts/', include('registration.backends.simple.urls', namespace='registration')),
    url(r'', include('rango.urls', namespace='rango')),

)

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

您需要更改urls.py文件以添加扩展默认RegistrationView的类:

from registration.backends.simple.views import RegistrationView    
class MyRegistrationView(RegistrationView):
        def get_success_url(self, request, user):
            return '/rango/'

然后更新您的网址格式,使其如下所示:

url(r'^accounts/register/$', MyRegistrationView.as_view(), name='registration_register')

我希望这会有所帮助。

答案 1 :(得分:0)

我在阅读本书时尝试使用命名空间,但我也遇到过这个错误。从项目的 urls.py 中删除namespace='registration'并在所有模板中从{% url 'registration:REGISTRATION_VIEW_NAME' %}更改为{% url 'REGISTRATION_VIEW_NAME' %}已解决了该问题。现在一切正常。