Python 如何使用内置的';密码重置';在Django中查看?
我在url.py中设置了以下条目Python 如何使用内置的';密码重置';在Django中查看?,python,django,django-1.4,Python,Django,Django 1.4,我在url.py中设置了以下条目 (r'^password_reset/$', 'django.contrib.auth.views.password_reset'), 但一旦我进入http://127.0.0.1:8000/password_reset/我收到错误消息: NoReverseMatch at /password_reset/ Reverse for 'django.contrib.auth.views.password_reset_done' with arguments '(
(r'^password_reset/$', 'django.contrib.auth.views.password_reset'),
但一旦我进入http://127.0.0.1:8000/password_reset/
我收到错误消息:
NoReverseMatch at /password_reset/
Reverse for 'django.contrib.auth.views.password_reset_done' with arguments '()' and keyword arguments '{}' not found.
我期待着密码重置完成视图也会出现。那么在这个阶段我应该做什么呢
更新
在尝试了布莱尔的解决方案后,我更进一步了
(r'^password_reset_done/$', 'django.contrib.auth.views.password_reset_done'),
根据《Django 1.0网站开发》一书的说法,这些内置视图应该在开箱即用的情况下使用,而不会带来更多麻烦。但也许自从Django 1.0以来它已经改变了。。。
如果有人能说明这一点,那就太好了。谢谢Django需要知道在用户完成密码重置页面上的表单后,将用户重定向到哪个URL。因此,在URL配置中添加另一行:
(r'^password_reset_done/$', 'django.contrib.auth.views.password_reset_done'),
我终于找到了解决办法。我认为MVC和MTV模式之间总是有一些误解。在MTV(Django)中,视图代表控制器,模板代表视图 因此,虽然更改密码“视图”是内置的,但实际的模板(外观和感觉)仍然需要由用户生成,而底层表单(小部件)则由Django自动生成。当查看代码时,它会变得更加清晰 因此,将这两行添加到url.py
(r'^change-password/$', 'django.contrib.auth.views.password_change'),
(r'^password-changed/$', 'django.contrib.auth.views.password_change_done'),
from django.contrib.auth import views as auth_views
...
url('^account/change-password/$',
auth_views.PasswordChangeView.as_view(
template_name='registration/passwd_change_form.html'),
name='password_change'),
url(r'^account/password-change-done/$',
auth_views.PasswordChangeDoneView.as_view(
template_name='registration/passwd_change_done.html'),
name='password_change_done'),
然后在myproject/templates/registration下添加这两个文件
password\u change\u done.html
{% extends "base.html" %}
{% block title %}Password Change Successful{% endblock %}
{% block head %}Password Change Completed Successfully{% endblock %}
{% block content %}
Your password has been changed successfully. Please re-login with your new credentials
<a href="/login/">login</a> or go back to the
<a href="/">main page</a>.
{% endblock %}
{% extends "base.html" %}
{% block title %}Change Registration{% endblock %}
{% block head %}Change Registration{% endblock %}
{% block content %}
<form method="post" action=".">
{{form.as_p}}
<input type="submit" value="Change" />
{% csrf_token %}
</form>
{% endblock %}
{%extends“base.html”%}
{%block title%}密码更改成功{%endblock%}
{%block head%}密码更改已成功完成{%endblock%}
{%block content%}
您的密码已成功更改。请使用新凭据重新登录
还是回到家里去
.
{%endblock%}
密码更改表单.html
{% extends "base.html" %}
{% block title %}Password Change Successful{% endblock %}
{% block head %}Password Change Completed Successfully{% endblock %}
{% block content %}
Your password has been changed successfully. Please re-login with your new credentials
<a href="/login/">login</a> or go back to the
<a href="/">main page</a>.
{% endblock %}
{% extends "base.html" %}
{% block title %}Change Registration{% endblock %}
{% block head %}Change Registration{% endblock %}
{% block content %}
<form method="post" action=".">
{{form.as_p}}
<input type="submit" value="Change" />
{% csrf_token %}
</form>
{% endblock %}
{%extends“base.html”%}
{%block title%}更改注册{%endblock%}
{%block head%}更改注册{%endblock%}
{%block content%}
{{form.as_p}}
{%csrf_令牌%}
{%endblock%}
从django 1.11开始,密码更改视图已被弃用 自版本1.11起已弃用:基于密码更改函数的视图应替换为基于类的PasswordChangeView 对我起作用的是: 在url.py中
(r'^change-password/$', 'django.contrib.auth.views.password_change'),
(r'^password-changed/$', 'django.contrib.auth.views.password_change_done'),
from django.contrib.auth import views as auth_views
...
url('^account/change-password/$',
auth_views.PasswordChangeView.as_view(
template_name='registration/passwd_change_form.html'),
name='password_change'),
url(r'^account/password-change-done/$',
auth_views.PasswordChangeDoneView.as_view(
template_name='registration/passwd_change_done.html'),
name='password_change_done'),
然后在注册下添加两个模板passwd\u change\u form.html和passwd\u change\u done.html
请注意,我没有使用默认名称,因为某种原因,当我这样做时,它默认为django admin视图 我已经按照你的建议做了,现在它说:templatedoesnotexistat/password\u reset/。。。异常值:registration/password\u reset\u form.html如果我必须自己创建模板,那么对我来说没有多大意义。我在这里遗漏了什么吗?不完全确定,我总是创建自定义模板,以便它们与网站其余部分的主题相适应。话虽如此,django.contrib.admin应用程序中似乎有一些匹配的模板,因此如果您启用了django admin,它们可能会被使用?为什么有人在没有任何解释的情况下对此投了-1票?这是一个有效的问题!!!谢谢你提到这个警告。这也是
password\u reset
的情况,它被PasswordResetView
替换,我会工作的。