Django 1.6和django-registration:内置认证视图未被识别

19
我正在尝试将我的Web应用从Django 1.5升级到Django 1.6,作为我的一组Django应用程序的一部分,我使用django-registration 1.0

升级到Django 1.6后,我的应用程序不再识别内置认证视图。 它们已集成在django registration中,可以看到这里, 但它们停止工作了。

Django发布说明描述了这些视图应如何集成的更改, 将其与注册应用程序中的源代码进行比较,看起来很好。

我按照以下方式引入注册URL:

urlpatterns = patterns('',
     ...,
     url(r'^accounts/', include('registration.backends.default.urls')),
)

当请求内置的url时,例如/accounts/password/change/,我会收到一个错误。

django.core.urlresolvers.NoReverseMatch

NoReverseMatch: Reverse for 'password_change_done' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

有人知道我为什么会收到“no-reverse-match”错误吗?

3个回答

35

出现这个错误的原因是 django.contrib.auth.views 使用不同的 URL 名称,而不是 registration.auth_urls。为了解决这个问题,在 django-registration 更新以适应 Django 1.6 之前,请覆盖默认的 URL,并使用与 Django 相同的名称。

from django.contrib.auth import views as auth_views


urlpatterns = patterns('',

      #override the default urls
      url(r'^password/change/$',
                    auth_views.password_change,
                    name='password_change'),
      url(r'^password/change/done/$',
                    auth_views.password_change_done,
                    name='password_change_done'),
      url(r'^password/reset/$',
                    auth_views.password_reset,
                    name='password_reset'),
      url(r'^password/reset/done/$',
                    auth_views.password_reset_done,
                    name='password_reset_done'),
      url(r'^password/reset/complete/$',
                    auth_views.password_reset_complete,
                    name='password_reset_complete'),
      url(r'^password/reset/confirm/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/$',
                    auth_views.password_reset_confirm,
                    name='password_reset_confirm'),

      #and now add the registration urls
      url(r'', include('registration.backends.default.urls')),
)

我将Jay上面列出的代码添加到了我的registration/backends/simple/urls.py文件中。如果您使用默认设置,您应该将其添加到registration/backends/default/urls.py文件中。这对我起作用了。 - teewuane
我也这样做了,而且对我有效,但是我的模板被忽略了,Django管理模板被用于密码重置和更改密码。 - Peter H
3
在django-registration存储库中,已经有一个开放的拉取请求来解决此问题。我认为它将会被整合到下一个版本中。请在这里跟踪讨论:https://bitbucket.org/ubernostrum/django-registration/pull-request/63/ - yellowcap
Macropin的django-registration fork已经修复了这个问题。请执行以下命令:pip uninstall django-registration,然后执行pip install -e git+https://github.com/macropin/django-registration#egg=django-registration - Rob Grant
我已询问Django团队是否愿意支持django-registration,因为提供用户/认证功能似乎没有注册支持不完整,但他们目前似乎不太热衷: https://groups.google.com/forum/#!topic/django-developers/yawNAWE4d1o - Rob Grant

8

这是我使用的内容:

url(r'', include('registration.backends.default.urls')),
url(r'', include('django.contrib.auth.urls')),

Django的贡献现在包括了缺失的URL。

目前这对我来说运行得很好。不过我会继续关注它。 - Rob Grant

0

背景

这个问题似乎又在django-registration==1.0和Django==1.6上出现了,并且在官方django-registration pull request here 中有记录。我使用了@Jay提供的解决方案,但它并没有特别针对密码重置部分起作用。最终我遇到了这个错误:

错误

password_reset_confirm() got an unexpected keyword argument 'uidb36'

解决方案

我选择从另一个django-registration存储库中获取(如上面的官方拉取请求中所提到的),具体操作如下:

  1. pip uninstall django-registration
  2. pip install git+git://github.com/macropin/django-registration.git
  3. 按照@Jay在帖子中提到的修改'urls.py'的代码(感谢!)
  4. 请记住,这只是一个临时解决方案,直到官方django-registration支持更新为Django 1.6

Django-registration不再维护了,是吗?(我更希望它成为Django的官方部分,而不是South,但也许这是1.8版本的事情!) - Rob Grant
我希望有人接手django-registration(或者也许不会)。我将我的升级到了1.6,由于其他一切都正常,所以我使用了分支来解决这个问题。 - Will

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接