2016-08-20 23 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視圖解決這個問題