【问题标题】:django password_change noreversematchdjango password_change noreversematch
【发布时间】:2016-11-09 01:48:09
【问题描述】:

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

当我点击“更改密码”链接时,我得到:

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

我知道“更改密码完成”链接很愚蠢,但我添加它是为了看看会发生什么。它工作正常。当我点击它时,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: []

我在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 错误。

知道我在“更改密码”方面做错了什么吗?

谢谢, 迈克

【问题讨论】:

    标签: python django authentication passwords


    【解决方案1】:

    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

    【讨论】:

    • 这让我继续前进,谢谢!我为 password_change_done 添加了 URL 声明,效果很好。
    猜你喜欢
    • 2017-09-07
    • 2014-02-10
    • 2017-05-21
    • 2013-09-26
    • 2013-01-19
    • 2016-07-20
    • 2017-04-18
    相关资源
    最近更新 更多