7

我在 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 以来它已经改变了......如果有人能阐明这一点,那就太好了。谢谢

4

3 回答 3

3

我终于找到了解决方案。我认为 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'),

然后在 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 %}

在此处输入图像描述

于 2012-05-14T09:53:43.363 回答
2

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

(r'^password_reset_done/$', 'django.contrib.auth.views.password_reset_done'),
于 2012-05-12T22:32:05.263 回答
1

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

然后在注册下添加一对模板passwd_change_form.htmlpasswd_change_done.html

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

于 2017-08-29T15:29:46.963 回答