django項目中使用郵箱找回密碼功能

本文使用qq郵箱,須要登陸郵箱,在設置-帳戶裏面開啓SMTP服務,要記下受權碼javascript

前端htmlhtml

{#找回密碼的表單#}

<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> {#找回密碼的表單結束#}

前端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') }) }); {# 找回密碼的jquery結束 #}

路由java

# 找回密碼
re_path('findpwd/', login.findpwd,name='findpwd'),

方法jquery

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 = threading.Thread(target=send_mail, args=( '%s在找回密碼' % member_name, '您的密碼已經被重置,新密碼爲%s' % num, settings.EMAIL_HOST_USER, ['%s' % member_obj.member_email] )) t.start() else: res['status'] = 1 res['info'] = '密碼找回失敗'
            return HttpResponse(json.dumps(res)) else: res['status'] = 3 res['info'] = '用戶/郵箱不存在'
        return HttpResponse(json.dumps(res)) return render(request, 'blog/findPwd.html')

項目目錄下建立untils文件夾,文件夾下建立function.py文件,封裝生成的隨機密碼django

# 隨機數
def range_num(num): # 定義一個種子,從這裏面隨機拿出一個值,能夠是字母
    seeds = "1234567890"
    # 定義一個空列表,每次循環,將拿到的值,加入列表
    random_num = [] # choice函數:每次從seeds拿一個值,加入列表
    for i in range(num): random_num.append(random.choice(seeds)) # 將列表裏的值,變成四位字符串
    return "" . join(random_num)#5454

settings.py文件中設置json

EMAIL_HOST='smtp.qq.com' EMAIL_PORT=465 EMAIL_HOST_USER='3028981710@qq.com'#帳號
EMAIL_HOST_PASSWORD='kimtvyyymbwodgaf'#密碼(受權碼)
EMAIL_USE_SSL=True

頁面效果app

done。dom

相關文章
相關標籤/搜索