使用Django自带用户管理器实现重置密码功能

业务逻辑

开始–请求重置密码–输入邮箱–发送邮件提示信息–阅读邮件点击超链接–重置密码–结束

因为使用的django的auth_views ,所以基本没有自己的业务逻辑,仅仅是将前端页面实现一遍,所以没有什么好说的

url配置

account app下urls.py 配置

urlpattern[
    path('password-reset/', auth_views.PasswordResetView.as_view(template_name="account/password_reset_form.html", email_template_name="account/password_reset_email.html", 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'),
]

页面配置

password_reset_email.html 代码片段

<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>

password_reset_done.html 代码片段

<p>Please go to the following page and chosse 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>

password_reset_done.html 代码片段

<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>

password_reset_confirm.html 代码片段

<div class="row text-center vertical-middle-sm">
    <h1>Reset Password</h1>
    <p>Please enter your new password trice so we can verify you tyoped 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>

password_reset_complete.html 代码片段

<div class="row text-center vertical-middle-sm">
    <h1>Reset your password</h1>
    <p>Your password has been set. you may go ahead and log in now</p>
</div>

settings.py 变量配置

EMAIL_HOST = 'smtp.126.com'
EMAIL_HOST_USER = "XXXXXX@126.com"
EMAIL_HOST_PASSWORD = "xxxxxxx"
EMAIL_PORT = 25
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = "XXXXXX@126.com"

ps: EMAIL_HOST_USER 必须与DEFAULT_FROM_EMAIL相同

实际结果

重置密码页面
重置密码页
发送邮件成功页面
发送密码成功
重置密码页
重置密码页面
密码重置成功页面
密码重置成功

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值