2016-07-06 88 views
0

我是新來的Django,我試圖添加用戶身份驗證到一個簡單的應用程序。我正在使用Django 1.9,我正在儘可能簡單地做到這一點。登錄和註銷工作,但使用「更改密碼」,我得到一個NoReverseMatch的password_change_done錯誤。django password_change noreversematch

從我的urls.py

app_name = 'league' 
urlpatterns = [ 
    url(r'^$', views.index, name='index'), 
    url('^', include('django.contrib.auth.urls')), 
] 

在/league/templates/league/index.html,我有:

<a href="{% url 'league:login' %}">Login</a> 
<a href="{% url 'league:logout' %}">Logout</a> 
<a href="{% url 'league:password_change' %}">Change Password</a> 
<a href="{% url 'league:password_change_done' %}">Change Password Done</a> 

我在創建這些文件/聯盟/模板/註冊/ 。 password_change_form.html和password_change_done.html目前什麼都不做,它們只包含一個顯示的字符串。

  • 的login.html
  • logged_out.html
  • password_change_form.html
  • password_change_done.html

當我點擊 「更改密碼」 鏈接,我得到:

NoReverseMatch at /league/password_change/ 
Reverse for 'password_change_done' with arguments'()' and keyword arguments '{}' not found. 

我知道「更改密碼已完成」鏈接是sil但是我添加了它來查看會發生什麼。它工作正常。當我點擊它時,按預期顯示password_change_done.html。

下面是當我點擊「更改密碼」鏈接的堆棧跟蹤:

Environment: 


Request Method: GET 
Request URL: http://localhost:8000/league/password_change/ 

Django Version: 1.9.2 
Python Version: 3.4.3 
Installed Applications: 
['league.apps.LeagueConfig', 
'django.contrib.admin', 
'django.contrib.auth', 
'django.contrib.contenttypes', 
'django.contrib.sessions', 
'django.contrib.messages', 
'django.contrib.staticfiles'] 
Installed Middleware: 
['django.middleware.security.SecurityMiddleware', 
'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 "/home/mriley/.virtualenvs/django1_8/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response 
    149.      response = self.process_exception_by_middleware(e, request) 

File "/home/mriley/.virtualenvs/django1_8/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response 
    147.      response = wrapped_callback(request, *callback_args, **callback_kwargs) 

File "/home/mriley/.virtualenvs/django1_8/lib/python3.4/site-packages/django/views/decorators/debug.py" in sensitive_post_parameters_wrapper 
    76.    return view(request, *args, **kwargs) 

File "/home/mriley/.virtualenvs/django1_8/lib/python3.4/site-packages/django/utils/decorators.py" in _wrapped_view 
    149.      response = view_func(request, *args, **kwargs) 

File "/home/mriley/.virtualenvs/django1_8/lib/python3.4/site-packages/django/contrib/auth/decorators.py" in _wrapped_view 
    23.     return view_func(request, *args, **kwargs) 

File "/home/mriley/.virtualenvs/django1_8/lib/python3.4/site-packages/django/contrib/auth/views.py" in inner 
    49.   return func(*args, **kwargs) 

File "/home/mriley/.virtualenvs/django1_8/lib/python3.4/site-packages/django/contrib/auth/views.py" in password_change 
    308.   post_change_redirect = reverse('password_change_done') 

File "/home/mriley/.virtualenvs/django1_8/lib/python3.4/site-packages/django/core/urlresolvers.py" in reverse 
    600.  return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))) 

File "/home/mriley/.virtualenvs/django1_8/lib/python3.4/site-packages/django/core/urlresolvers.py" in _reverse_with_prefix 
    508.        (lookup_view_s, args, kwargs, len(patterns), patterns)) 

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

我發現計算器類似案件,其中包括this one.

我改變了我的urls.py這樣:

url(r'^password_change/$', 
    auth_views.password_change, 
    {'current_app': 'league'}, 
    name='password_change'), 
url(r'^password_change_done/$', 
    auth_views.password_change_done, 
    {'current_app': 'league'}, 
    name='password_change_done'), 

但它沒有區別,我仍然得到NoReverseMatch錯誤。

任何想法我做錯了「更改密碼」?

謝謝, 邁克

回答

0

password_change視圖does not usecurrent_app參數來確定重定向URL。你需要明確傳遞視圖名稱,包括命名空間:

url(r'^password_change/$', 
    auth_views.password_change, 
    {'post_change_redirect': 'league:password_change_done'}, 
    name='password_change'), 

current_app參數只在模板中的{% url %}標籤使用,但它是deprecated,並會在Django 2.0中刪除。如果您需要模板上下文中的當前應用程序,則需要設置request.current_app

+0

這讓我走了,謝謝!我添加了password_change_done的URL聲明,並且它工作得很好。 – Mike