【发布时间】:2015-08-29 13:06:49
【问题描述】:
背景
我正在尝试自定义 Django 项目中的身份验证视图,但我似乎无法运行自定义的 password_change 视图。我使用 Django 1.8.2 和 Python 2.7。
我的模块userauth 的urls.py 如下所示:
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 %}
问题
当我打开'password_change_done' 页面(在/account/password-change-done)时,一切都很好。
但是在'password-change' (/accunt/password-change) 我收到了这个错误:
NoReverseMatch 位于 /account/password-change/
未找到带有参数“()”和关键字参数“{}”的“password_change_done”的反向操作。尝试了 0 个模式:[]
我尝试了什么
我不知道为什么会这样。
- 我尝试从
url 'userauth_password_change'中删除单引号 - 我确保
password-change-donepage 存在于 urls.py 中并且可用 - 我阅读了Reverse for '*' with arguments '()' and keyword arguments '{}' not found、Django: Reverse for 'detail' with arguments '('',)' and keyword arguments '{}' not found、Django change_password NoReverseMatch at /accounts/password/change/ 上的解决方案(还有一些,我尝试了那里的所有解决方案,但我在自己的代码中找不到问题)
感谢任何帮助。谢谢!
【问题讨论】:
-
当我将
name='userauth_password_change_done'的名称更改为name='password_change_done'时,它可以工作。这是为什么?有人可以解释一下吗?
标签: python django django-authentication