六、Django2.1 搭建多用户的博客网站——重置密码

96
麦典威
2019.01.27 21:45* 字数 997

目录:Django 2.1 从零开始搭建博客网站系列

服务器环境搭建(选学)

小试牛刀——简单的博客网站

庖丁解牛——多用户的博客网站之用户模块

庖丁解牛——多用户的博客网站之文章模块

华丽转身——多用户的博客网站之扩展功能

项目源码下载:https://github.com/jt1024/lehehe

正文:

如果忘记密码了,就只能重置密码了。Django提供了内置的重置密码方法。

1、重置密码的基本流程

选择重置密码—>填写注册用户时的邮箱—>向用户发送邮件—>点击右键中的连接—>进入重置密码页面—>重置密码成功

2、使用内置方法重置密码

在 ./account/urls.py 中新增如下代码

path('password-reset/',
         auth_views.PasswordResetView.as_view(
             template_name="account/password_reset_form.html",
             email_template_name="account/password_reset_email.html",
             subject_template_name="account/password_reset_subject.txt",
             success_url="/account/password-reset-done/"),
         name='password_reset'),
    path('password-reset-done/', auth_views.PasswordResetDoneView.as_view(template_name="account/password_reset_done.html"), name='password_reset_done'),
    path('password-reset-confirm/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(template_name="account/password_reset_confirm.html", success_url='/account/password-reset-complete/'),
         name="password_reset_confirm"),
    path('password-reset-complete/', auth_views.PasswordResetCompleteView.as_view(template_name='account/password_reset_complete.html'), name='password_reset_complete'),

创建 ./templates/account/password_reset_form.html

{% extends "base.html" %}
{% block title %}password reset{% endblock %}

{% block content %}
<div class="row text-center vertical-middle-sm">
    <h1>Forgotten your password? Reset, please.</h1>
    <p>Enter your email to set a new password.</p>
    <form class="form-horizontal" action="." method="post">{% csrf_token %}
        <div class="form-group">
            <label class="col-md-5 control-label text-right">Email</label>
            <div class="col-md-6 text-left">{{ form.email }}</div>
        </div>
        <input type="submit" value="Send email" class="btn btn-primary btn-lg"></form>
</div>
{% endblock %}

创建 ./templates/account/password_reset_email.html

<p>You're receiving this email because you requested a password reset for your user account at taoge100.com </p>
<p>Please go to the following page and choose a new password:</p>
{{ protocol }}://{{ domain }}{% url 'account:password_reset_confirm' uidb64=uid token=token %}
<p>Your username, in case you've forgotten:{{ user.get_username }}</p> <p>Thanks for using our site!</p>
<p>The taoge100.com team</p>

创建 ./templates/account/password_reset_subject.txt

{% load i18n %}{% autoescape off %}
{% blocktrans %}Password reset on {{ site_name }}{% endblocktrans %}
{% endautoescape %}

创建 ./templates/account/password_reset_done.html

{% extends "base.html" %}
{% block title %}password reset{% endblock %}

{% block content %}
<div class="row text-center vertical-middle-sm">
    <h1>Reset your password</h1>
    <p>We've emailed you instructions for setting your password, if an account exists with the email you entered. You should receive them shortly.</p>
    <p>If you don't receive an email, please make sure you've entered the address you registered with, and check your spam folder.</p>
</div>
{% endblock %}

创建 ./templates/account/password_reset_confirm.html

{% extends "base.html" %}
{% block title %}password reset{% endblock %}

{% block content %}
<div class="row text-center vertical-middle-sm">
    <h1>Reset Password</h1>
    <p>Please enter your new password twice so we can verify you typed it in correctly.</p>
    <form class="form-horizontal" action="." method="post">{% csrf_token %}
        <div class="form-group">
            <label class='col-md-5 control-label text-right'>New password</label>
            <div class="col-md-6 text-left">{{ form.new_password1 }}</div>
        </div>
        <div class="form-group">
            <label class='col-md-5 control-label text-right'>Confirm password</label>
            <div class="col-md-6 text-left">{{ form.new_password2 }}</div>
        </div>
        <input type="submit" value="Change my password" class="btn btn-primary btn-lg"/>
    </form>
</div>
{% endblock %}

创建 ./templates/account/password_reset_complete.html

{% extends "base.html" %}
{% block title %}password reset{% endblock %}

{% block content %}
<div class="row text-center vertical-middle-sm">
    <h1>Reset your password</h1>
    <p>Your password has been set. You may go ahead and <a href="{% url 'account:user_login' %}">log in now</a>.
    </p>
</div>
{% endblock %}

在 ./lehehe/settings.py 中配置邮箱参数,在末尾增加如下代码

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.163.com'
EMAIL_HOST_USER = '邮箱名@163.com'
EMAIL_HOST_PASSWORD = '邮箱密码'
EMAIL_PORT = '25'
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = '邮箱名@163.com'

3、测试

先登录一个用户,然后通过浏览器访问 http://127.0.0.1:8000/account/password-reset/

填写接收重置密码的邮箱地址.png
发送邮件成功.png
收到的邮件.png
通过邮件链接进入重置密码的表单页面.png
重置密码成功.png

4、遗留问题

问题1

发送邮件的时候,有时成功,有时失败,失败的时候会遇到如下报错:
(554, b'DT:SPM 163 smtp7,C8CowACHJHWask1cSwP_DA--.21615S3 1548595866,please see http://mail.163.com/help/help_spam_16.htm?ip=1.203.182.8&hostid=smtp7&time=1548595866')

163邮箱的官方文档说明是:
554 DT:SPM 发送的邮件内容包含了未被许可的信息,或被系统识别为垃圾邮件。请检查是否有用户发送病毒或者垃圾邮件;

问题2

发送邮件成功的时候,会连续发送3封邮件


连续发送三封邮件.png

需要进一步优化,优化后会及时更新文章。