django password_change noreversematch

时间:2016-07-06 20:57:49

标签: python django authentication passwords

我是Django的新手,我正在尝试将用户身份验证添加到一个简单的应用中。我正在使用Django 1.9,我尽可能地尝试这样做。登录和注销工作,但使用"更改密码",我收到了password_change_done的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>

我在/ league / templates / registration /中创建了这些文件。 password_change_form.html和password_change_done.html目前什么都不做,它们只包含一个显示的字符串。

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

当我点击&#34;更改密码&#34;链接,我明白了:

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

我知道&#34;更改密码完成&#34;链接很傻,但我添加它看看会发生什么。它工作正常。当我点击它时,会按预期显示password_change_done.html。

这是我点击&#34;更改密码&#34;时的堆栈跟踪。链接:

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: []

我在stackoverflow上发现了类似的情况,包括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错误。

我对#34;更改密码&#34;错误地做了什么想法?

谢谢, 麦克

1 个答案:

答案 0 :(得分:0)

password_change查看does not use current_app参数以确定重定向网址。您需要显式传递视图名称,包括命名空间:

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