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

字数 976阅读 810

目录: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_USER_SSL = True
# 邮件服务器,如果是163,就改成 smtp.163.com
EMAIL_HOST = 'smtp.qq.com'
# 邮件服务器端口
EMAIL_PORT = 25
# 发送邮件的账号
EMAIL_HOST_USER = '邮箱名称'
# SMTP服务密码
EMAIL_HOST_PASSWORD = '你的SMTP服务密码,即邮箱的登录授权码'
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER

3、测试

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

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

4、疑难问题解决

问题1 :获取邮箱登录授权码

不同的邮箱登录授权码后去方式不同,可自行百度。比如获取QQ邮箱的SMTP服务密码,可参考 https://cloud.tencent.com/developer/article/1392336

问题2:邮件发送失败

发送邮件的时候,如果失败,参考:Django 发送邮件失败

问题3:同一个邮箱连续收到多封邮件

如果同一个邮箱连续收到多封邮件,是因为你的后台有多个用户注册的都是这一个邮箱

推荐阅读更多精彩内容