django project using the mailbox to retrieve the password function

As used herein, qq-mail, you need to log on the mailbox, in the setting - open SMTP service account inside, note that the authorization code

Html front end

{#找回密码的表单#}

<form action="" method="post" id="login">
    {% csrf_token %}
    <input type="text" class="name" name="member_name" Placeholder="Username" required=""/>
    <input type="text" class="email" name="member_email" Placeholder="email" required=""/>

    <div class="login-agileits-bottom">
        <h6><a href="javascript:;" type="button" id="onsubmit">提交</a></h6>
    </div>
</form>

{#找回密码的表单结束#}

The front js

{#  找回密码的jquery  #}
$(document).ready(function () {
    $('#onsubmit').click(function () {
        $.post('/blog/findpwd/', $('#login').serialize(), function (data) {
            if (data['status'] == 0) {
                layer.msg(data.info,{
                    time:2000
                });
                {#location.href = "/blog/login/";#}
            } else {
                layer.msg (Data [' Info ' ]) 
            } 
        }, ' JSON ' ) 
    }) 
}); 
{ #   retrieve the password} # End jquery

routing

# Retrieve password 
re_path ( ' findpwd / ' , login.findpwd, name = ' findpwd ' ),

method

def findpwd(request):
    from django.core.mail import send_mail
    from mysite7 import settings
    import threading
    from blog.utils import function  # 引入自定义的验证码
    if request.method == 'POST':
        res = {'status': None, 'info': None}
        member_name = request.POST.get('member_name')
        member_email = request.POST.get('member_email')
        member_obj = Member.objects.filter(member_name=member_name, member_email=member_email).first()
        
        if member_obj:
            # 随机生成新密码
            num = function.range_num(6)
            new_pwd = Member.objects.filter(member_name=member_name).update(member_pwd=make_password(num))
            if new_pwd:
                res['status'] = 0
                res['info'] = '邮件已发送,注意查收'
                T = of the threading.Thread (= target the send_mail, args = (
                     ' % S in the retrieve password ' % member_name,
                     ' Your password has been reset, the new password S% ' % NUM, 
                    settings.EMAIL_HOST_USER, 
                    [ ' % S ' % member_obj.member_email] 
                )) 
                t.start () 
            the else : 
                RES [ ' Status ' ]. 1 = 
                RES [ ' info ' ] = ' password recovery failure '
            return HttpResponse(json.dumps(res))
        else:
            res['status'] = 3
            res['info'] = '用户/邮箱不存在'
        return HttpResponse(json.dumps(res))
    return render(request, 'blog/findPwd.html')

Create a directory under the project untils folder, create function.py file folders, packages generated random password

# Random number 
DEF range_num (NUM):
     # define a seed, from the inside out a random value, can be alpha 
    Seeds = " 1234567890 " 
    # define an empty list, the value of each cycle, will get added to a list 
    random_num = []
     # Choice function: each take a value from seeds, to a list 
    for I in Range (NUM): 
        random_num.append (the random.choice (seeds)) 
    # the value of the list into four strings 
    return  "" . the Join (random_num) # 5454

settings.py file settings

= EMAIL_HOST ' smtp.qq.com ' 
EMAIL_PORT = 465 
EMAIL_HOST_USER = ' [email protected] ' # account 
EMAIL_HOST_PASSWORD = ' kimtvyyymbwodgaf ' # password (authorization code) 
EMAIL_USE_SSL = True

Results page

done。

Guess you like

Origin www.cnblogs.com/nmsghgnv/p/11366130.html