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

Django 使用系统重置密码功能

  • 业务逻辑
    • url配置
    • 页面配置
    • settings.py 变量配置
    • 实际结果

业务逻辑

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

因为使用的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///', 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">Emaillabel>
            <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 passwordh1>
    <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 folderp>
div>

password_reset_confirm.html 代码片段

<div class="row text-center vertical-middle-sm">
    <h1>Reset Passwordh1>
    <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 passwordlabel>
            <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 passwordlabel>
            <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 passwordh1>
    <p>Your password has been set. you may go ahead and log in nowp>
div>

settings.py 变量配置

EMAIL_HOST = 'smtp.126.com'
EMAIL_HOST_USER = "[email protected]"
EMAIL_HOST_PASSWORD = "xxxxxxx"
EMAIL_PORT = 25
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = "[email protected]"

ps: EMAIL_HOST_USER 必须与DEFAULT_FROM_EMAIL相同

实际结果

重置密码页面
使用Django自带用户管理器实现重置密码功能_第1张图片
发送邮件成功页面
发送密码成功
重置密码页
使用Django自带用户管理器实现重置密码功能_第2张图片
密码重置成功页面
使用Django自带用户管理器实现重置密码功能_第3张图片

你可能感兴趣的:(python,Django学习)