Django默认password_change不再工作了吗?

时间:2016-08-20 05:00:16

标签: django django-authentication

我尝试使用django内置身份验证视图添加change password功能。

我创建名为'用户'并配置urls.py,如下所示:

from django.conf.urls import url
from django.contrib.auth.views import login, logout, password_change, password_change_done

from .views import SignUpView


app_name = 'users'
urlpatterns = [
    url(r'^signup/$', SignUpView.as_view(), name='signup'),
    url(r'^login/$', login, name='login', kwargs={'template_name': 'users/login.html'}),
    url(r'^logout/$', logout, name='logout', kwargs={'next_page': '/'}),
    url(
        r'^password_change/$',
        password_change,
        name='password_change',
        kwargs={
            'template_name': 'users/password_change_form.html',
            'current_app':'users'
        }
    ),
    url(
        r'^password_change/done$',
        password_change_done,
        name='password_change_done',
        kwargs={
            'template_name': 'users/password_change_done.html',
            'current_app':'users'
        }
    ),
]

我也创建users/password_change_form.htmlusers/password_change_done.html

但它出现了错误:

[20/Aug/2016 13:55:00] "GET /accounts/password_change/ HTTP/1.1" 500 106496
Internal Server Error: /accounts/password_change/
Traceback (most recent call last):
  File "/Users/Chois/.pyenv/versions/chacha_dabang/lib/python3.5/site-packages/django/core/handlers/base.py", line 149, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/Chois/.pyenv/versions/chacha_dabang/lib/python3.5/site-packages/django/core/handlers/base.py", line 147, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/Chois/.pyenv/versions/chacha_dabang/lib/python3.5/site-packages/django/views/decorators/debug.py", line 76, in sensitive_post_parameters_wrapper
    return view(request, *args, **kwargs)
  File "/Users/Chois/.pyenv/versions/chacha_dabang/lib/python3.5/site-packages/django/utils/decorators.py", line 149, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/Users/Chois/.pyenv/versions/chacha_dabang/lib/python3.5/site-packages/django/contrib/auth/decorators.py", line 23, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/Users/Chois/.pyenv/versions/chacha_dabang/lib/python3.5/site-packages/django/contrib/auth/views.py", line 49, in inner
    return func(*args, **kwargs)
  File "/Users/Chois/.pyenv/versions/chacha_dabang/lib/python3.5/site-packages/django/contrib/auth/views.py", line 308, in password_change
    post_change_redirect = reverse('password_change_done')
  File "/Users/Chois/.pyenv/versions/chacha_dabang/lib/python3.5/site-packages/django/core/urlresolvers.py", line 600, in reverse
    return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
  File "/Users/Chois/.pyenv/versions/chacha_dabang/lib/python3.5/site-packages/django/core/urlresolvers.py", line 508, in _reverse_with_prefix
    (lookup_view_s, args, kwargs, len(patterns), patterns))
django.core.urlresolvers.NoReverseMatch: Reverse for 'password_change_done' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

需要你的帮助。感谢:)

1 个答案:

答案 0 :(得分:3)

问题是您在网址配置的顶部指定了app_name。这意味着您定义的所有URL都使用app_name命名空间。因此,密码更改完成网址的最终名称为users:password_change_done,而不是password_change_done。这就是反向查找失败的原因。

您可以通过将post_change_redirect kwarg传递到password_change视图来解决此问题:

from django.core.urlresolvers import reverse

url(
    r'^password_change/$',
    password_change,
    name='password_change',
    kwargs={
        'template_name': 'users/password_change_form.html',
        'current_app':'users',
        'post_change_redirect': reverse_lazy('users:password_change_done')
    }
),

另请注意,自Django 1.9以来current_app参数为deprecated

  

不推荐使用current_app参数,将在Django 2.0中删除。来电者应该设置request.current_app