2016-08-20 19 views
1

我试图用户django内置auth视图添加change password函数。Django的默认password_change不起作用了吗?

我创建了一个名为 '用户' 应用程序和配置urls.py象下面这样:

而且我也创建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: [] 

需要你的帮助。谢谢:)

回答

3

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

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') 
    } 
), 

还要注意current_app说法是因为Django的1.9 deprecated

The current_app parameter is deprecated and will be removed in Django 2.0. Callers should set request.current_app instead.

你可以通过一个post_change_redirect kwarg到password_change视图解决这个问题