【问题标题】:How to use the built-in 'password_reset' view in Django?如何在 Django 中使用内置的“密码重置”视图?
【发布时间】:2012-05-12 22:22:10
【问题描述】:

我在 urls.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 '()' and keyword arguments '{}' not found.

我期待 password_reset_done 视图也能开箱即用。那么在这个阶段我应该怎么做呢?

更新

在尝试了 Blair 的解决方案后,我又近了一步。

(r'^password_reset_done/$', 'django.contrib.auth.views.password_reset_done'),

根据“Django 1.0 网站开发”一书,这些内置视图应该开箱即用,没有更多麻烦。但也许自 Django 1.0 以来它已经改变了...... 如果有人能阐明这一点,那就太好了。谢谢

【问题讨论】:

  • 为什么有人在没有任何解释的情况下对此投票-1?这是一个有效的问题!!!

标签: python django django-1.4


【解决方案1】:

我终于找到了解决办法。我认为 MVC 和 MTV 模式之间总是存在细微的误解。在 MTV (Django) 中,View 代表控制器,Template 代表 View。

因此,虽然更改密码“视图”是开箱即用的内置功能,但实际模板(外观和感觉)仍然需要由用户生成,而底层表单(小部件)由 Django 自动生成。看代码就更清楚了。

因此将这两行添加到 url.py

(r'^change-password/$', 'django.contrib.auth.views.password_change'), 
(r'^password-changed/$', 'django.contrib.auth.views.password_change_done'),

然后在myproject/templates/registration下添加这两个文件

password_change_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 %}

password_change_form.html

{% 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 %}

【讨论】:

    【解决方案2】:

    一旦用户完成密码重置页面上的表单,Django 需要知道将用户重定向到哪个 URL。因此,在您的 URL 配置中添加另一行:

    (r'^password_reset_done/$', 'django.contrib.auth.views.password_reset_done'),
    

    【讨论】:

    • 我已经按照你的建议做了,现在它说: TemplateDoesNotExist at /password_reset/ ... 异常值:registration/password_reset_form.html 如果我必须创建模板,这对我没有多大意义我。我在这里错过了什么吗?
    • 不完全确定,我总是创建自定义模板,以便它们适合网站其余部分的主题。话虽如此,django.contrib.admin 应用程序中似乎有一些匹配的模板,所以如果您启用了 Django 管理员,它们可能会被使用?
    【解决方案3】:

    从 django 1.11 开始,password_change 视图已被弃用。

    1.11 版后已弃用:基于 password_change 函数的视图应替换为基于类的 PasswordChangeView。

    对我有用的是:

    urls.py

    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'),
    

    然后在registration下添加passwd_change_form.htmlpasswd_change_done.html这两个模板。

    请注意,我没有使用默认名称,出于某种原因,当我这样做时,它默认为 django 管理视图。

    【讨论】:

    • 感谢您提及警告。 password_reset 也是这种情况,它被 PasswordResetView 取代,我会工作的。
    猜你喜欢
    • 2013-12-16
    • 2011-11-22
    • 1970-01-01
    • 1970-01-01
    • 2020-04-16
    • 1970-01-01
    • 2021-05-28
    • 2020-04-26
    • 1970-01-01
    相关资源
    最近更新 更多