django密码重置password_reset_confirm

时间:2014-01-22 13:46:58

标签: python django

我在django做一个项目。在我的项目中,我需要通过电子邮件重置密码。为此,我使用了django.contrib.auth.urls。我通过djangos模板测试成功。后来我在我的模板上试了一下,因为我创建了一个注册文件夹并创建了五个html页面,包括电子邮件模板。我还在我的主网址中加入了url(r'^web/',include('django.contrib.auth.urls'))。但是当我运行服务器时,我可以将链接传递给电子邮件。并在我的浏览器中打开视图。在那里我改变了我输入两次的密码,当我提交什么都没有发生什么问题请帮助我?

我的password_reset_confirm.html

{% extends 'base.html' %}
{% load i18n %}

{% block content %}
    {% if validlink %}
        <p>Please enter your new password twice.<br />
           So we can verify you typed it in correctly.</p>
        <form action="" method="post">
            <div style="display:none">
                <input type="hidden" value="{{ csrf_token }}"name="csrfmiddlewaretoken">
            </div>
            <table>
                <tr>
                    <td>{{ form.new_password1.errors }}
                        <label for="id_new_password1">New password:</label></td>
                    <td>{{ form.new_password1 }}</td>
                </tr>
                <tr>
                    <td>{{ form.new_password2.errors }}
                        <label for="id_new_password2">Confirm password:</label></td>
                    <td>{{ form.new_password2 }}</td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="submit" value="Change my password" /></td>
                </tr>
            </table>
        </form>
    {% else %}
        <h1>Password reset unsuccessful</h1>
        <p>The password reset link was invalid, <br />
        possibly because it has already been used. <br />
        Please request a new password reset.</p>
    {% endif %}
{% endblock %}

1 个答案:

答案 0 :(得分:2)

我用一种不同的方式实现了密码重置程序,波纹管已经过测试并且有效:

项目中的

urls.py:

urlpatterns = patterns('',
    url(r'^user/', include('users.urls')),
)
在urls.py中

...
url(r'^password-reset/$', 'django.contrib.auth.views.password_reset', kwargs={'template_name': 'password_reset_form.html'}, name='password-reset'),
url(r'^password_reset/done/$', 'django.contrib.auth.views.password_reset_done', kwargs={'template_name': 'password_reset_done.html'}, name='password_reset_done'),
url(r'^reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$','django.contrib.auth.views.password_reset_confirm',kwargs={'template_name': 'password_reset_confirm.html'},name='password_reset_confirm'),
url(r'^reset/done/$', 'django.contrib.auth.views.password_reset_complete', kwargs={'template_name': 'password_reset_complete.html'}, name='password_reset_complete'),
...

为了我的方便,我在应用程序的根目录下创建了模板文件,上面的网址在我项目的/ user下。

users/templates$ ls password*
password_reset_complete.html  password_reset_done.html
password_reset_confirm.html   password_reset_form.html

我测试了它的工作,表单与django提供的表单相同,只是我扩展了我的首页基础模板,因为我不希望任何django管理模板出现在前端用户,例如:

{% extends "base.html" %}
{% load i18n %}

{% block title %}{% trans 'Password reset' %}{% endblock %}

{% block maincontent %}
<div class="row">
    <div class="col-lg-6 col-lg-offset-3">
        {% if validlink %}
        <h1>{% trans 'Enter new password' %}</h1>
        <p>{% trans "Please enter your new password twice so we can verify you typed it in correctly." %}</p>

        <form action="" method="post">{% csrf_token %}
            {{ form.new_password1.errors }}
            <div class="form-group">
                <label for="id_new_password1">{% trans 'New password:' %}</label>
                <input class="form-control" type="password" id="id_new_password1" name="new_password1" value="">
            </p>
            {{ form.new_password2.errors }}
            <div class="form-group">
                <label for="id_new_password2">{% trans 'Confirm password:' %}</label>
                <input class="form-control" type="password" id="id_new_password2" name="new_password2" value="">
            </div>
            <div class="row">
                <div class="col-lg-l2">
                    <input type="submit" class="btn btn-default" value="{% trans 'Change my password' %}" />
                </div>
            </div>
        </form>

        {% else %}

        <h1>{% trans 'Password reset unsuccessful' %}</h1>

        <p>{% trans "The password reset link was invalid, possibly because it has already been used.  Please request a new password reset." %}</p>

        {% endif %}
    </div>
</div>
{% endblock maincontent %}