反向' password_change_done'参数'()'和关键字参数' {}'找不到

时间:2015-06-13 17:16:22

标签: python django django-authentication

背景

我正在尝试在Django项目中自定义身份验证视图,但我似乎无法运行自定义的password_change视图。我使用Django 1.8.2和Python 2.7。

我的模块urls.py的{​​{1}}如下所示:

userauth

这在主from django.conf.urls import patterns, include, url urlpatterns = patterns('django.contrib.auth.views', url(r'^login/$', 'login', {'template_name': 'userauth/login.html'}, name='userauth_login'), url(r'^logout/$', 'logout', {'next_page': '/'}, name='userauth_logout'), url(r'^password-change/$', 'password_change', {'template_name': 'userauth/password_change_form.html'}, name='userauth_password_change'), url(r'^password-change-done/$', 'password_change_done', {'template_name': 'userauth/password_change_done.html'}, name='userauth_password_change_done'), ) 中被引用为:

urls.py

我的urlpatterns = [ url(r'^admin/', include(admin.site.urls)), url(r'^account/', include('userauth.urls')), ]

的模板
userauth/password_change_form.html

{% extends "base.html" %} {% block title %}{{ block.super }} - Change Password{% endblock %} {% block toggle_login %}{% endblock %} {% block content %} <form action="{% url 'userauth_password_change' %}" method="post" accept-charset="utf-8"> {{ form.as_p }} {% csrf_token %} <input type="submit" value="Change password"/> </form> {% endblock %}

的模板
userauth/password_change_done.html

问题

当我打开{% extends "base.html" %} {% block title %}{{ block.super }} - Password change successful{% endblock %} {% block content %} <p>Your password has been changed successfully.</p> <a href="{% url 'products_product_index' %}">Back to your Account</a> {% endblock %} 页面(/ account / password-change-done)时,一切都很好。

但是在'password_change_done'(/ accunt / password-change)我收到了这个错误:

  

NoReverseMatch at / account / password-change /

     

反向&#39; password_change_done&#39;参数&#39;()&#39;和关键字参数&#39; {}&#39;未找到。尝试了0种模式:[]

我尝试了什么

我不知道为什么会发生这种情况。

  1. 我尝试从'password-change'
  2. 删除单引号
  3. 我确保urls.py中存在url 'userauth_password_change'页面并且可用
  4. 我在Reverse for '*' with arguments '()' and keyword arguments '{}' not foundDjango: Reverse for 'detail' with arguments '('',)' and keyword arguments '{}' not foundDjango change_password NoReverseMatch at /accounts/password/change/上阅读了解决方案(还有一些,我在那里尝试了所有解决方案,但我在我的网站上找不到问题自己的代码)
  5. 感谢任何帮助。谢谢!

6 个答案:

答案 0 :(得分:5)

好的,所以我建议的解决方案在这里不起作用。我在具有特定应用标签的应用程序中使用Django 1.8.8,因此我需要在模板中指定一个URL,例如: app_label:URL_NAME。这意味着password_change_done的反向将永远不会起作用,因为它是app_label:password_change_done。

但幸运的是有一个解决方案:'post_change_redirect'。因此我指定了password_change,如下所示:

url(r'^password_change$', 'django.contrib.auth.views.password_change', {'template_name': 'password_change.html', 'post_change_redirect': 'app_label:password_change_done'}, name='password_change'),

我确信其他人可以使用它来解决上面的问题,并且仍然保留自己的自定义网址名称。

答案 1 :(得分:3)

在某些部分中,您调用名为“password_change_done”的网址

正确的名称是:“userauth_password_change_done”

答案 2 :(得分:3)

解决方法是,在urls.py中,password_change_done链接的名称必须'password_change_done'

url(r'^password-change-done/$', 'password_change_done',
    {'template_name': 'userauth/password_change_done.html'},
    name='password_change_done'),

我查看了django.contrib.auth.views.password_change(正在创建问题)并意识到,在Django 1.8.2中,网址'password_change_done'是硬编码的。

答案 3 :(得分:1)

您需要删除视图名称周围的单引号

{% url password_change_done %}

而不是

{% url 'password_change_done' %}

答案 4 :(得分:0)

我遇到同样的问题。原因是“ password_change_done”是auth.views中的硬代码,因此我在auth.views.PasswordChangeView的基础上扩展了该类。

auth.views.PasswordChangeView:

class PasswordChangeView(PasswordContextMixin, FormView):
......
success_url = reverse_lazy('password_change_done')

MyPasswordChangeView:

class MyPasswordChangeView(PasswordChangeView):
    success_url = reverse_lazy('app_label:password_change_done')

仅覆盖success_url

并在urlpattern中使用MyPasswordChangeView。现在,一切正常。

答案 5 :(得分:0)

以防您使用

  

app_name

并尝试覆盖默认模板以更新/更改密码,您必须告诉Django:

from django.urls import path, reverse_lazy
from django.contrib.auth import views as auth_view
from . import views

app_name = 'account'

urlpatterns = [
    path('login/', auth_view.LoginView.as_view(), name='login'),
    path('logout/', auth_view.LogoutView.as_view(), name='logout'),

    path('password_change/',
         auth_view.PasswordChangeView.as_view(
             template_name='registration/password_change_form.html',
             success_url=reverse_lazy('account:password_change_done')), name='password_change'),

    path('password_change/done/',
         auth_view.PasswordChangeDoneView.as_view(
             template_name='registration/password_change_done.html'), name='password_change_done'),
]