python-Web-django-邮箱登陆

setting:

# 发送邮箱
EMAIL_HOST = 'smtp.163.com'
EMAIL_PORT = 465
EMAIL_HOST_USER = '666666@163.com'

#EMAIL_HOST_PASSWORD = '666666'
EMAIL_HOST_PASSWORD = '666666'
EMAIL_USE_SSL = True

 

until:

import random
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)

 

views:

from django.shortcuts import render,HttpResponse,redirect
from app01.models import *
from django.core.mail import send_mail
from my_pro import settings
import threading
import json
from app01.utils.function import range_num

def getpwd(request):
    '''忘记密码'''
    res = {'s':None,'info':None}
    if request.method == 'POST':
        ''''''
        try:
            username = request.POST.get('username')
            email = request.POST.get('email')
            member_obj = Member.objects.filter(member_name=username,member_email=email).first()
            if member_obj:
                '''发送邮件'''
                code = range_num(4)
                request.session['email_code'] = code
                liuyan = '用户{}获得密钥{}!如果非本人操作!请及时联系本站!'.format(username,code)
                tuple1 = ('邮箱密码查找信息:',
                          liuyan,
                          settings.EMAIL_HOST_USER,
                          ['666666@qq.com',])
                t = threading.Thread(target=send_mail,args=tuple1)
                t.start()
                print(liuyan)
                # send_mail('邮箱密码查找信息:',
                #           liuyan,
                #           settings.EMAIL_HOST_USER,
                #         ['666666@qq.com'])
                print(111)
                res['s'] = 1
                res['info'] = '邮箱发送成功!'
                return HttpResponse(json.dumps(res))
            else:
                res['s'] = 0
                res['info'] = '邮箱发送失败!'
                return HttpResponse(json.dumps(res))
        except EOFError as e:
            print(e)
            res['s'] = 0
            res['info'] = '输入错误'
            return HttpResponse(json.dumps(res))


    return render(request,'app01_get_pwd.html')

def repwd(request):
    '''更新密码'''
    res = {'s': None, 'info': None}
    if request.method == 'POST':
        ''''''
        try:
            username = request.POST.get('username')
            email = request.POST.get('email')
            pwd = request.POST.get('pwd')
            repwd = request.POST.get('repwd')
            if pwd == repwd:
                Member.objects.filter(member_name=username,member_email=email).first().update(member_pwd=pwd)
                res['s'] = 1
                res['info'] = '更新密码成功!'
                del request.session['email_code']
            else:
                res['s'] = 0
                res['info'] = '密码错误!'
        except:
            res['s'] = 0
            res['info'] = '输入错误1'
    return HttpResponse(json.dumps(res))

 

html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>忘记密码</title>
</head>
<body>
<a href="{% url 'app01:index' %}">首页</a>
<a href="{% url 'app01:login' %}">登陆页面</a>
<form action="#">
    {% csrf_token %}
    账号:<input type="text" name="username"><br>
    邮箱:<input type="text" name="email">
    <input type="button" value="发送邮件" id="sendemail"><br>
    <div class="maina">
    </div>

</form>

</body>
</html>
<script src="/static/js/jquery.min.js"></script>
<script src="/static/layer/layer.js"></script>
<script>

    $(document).on('click','#sendemail',function () {
        $.post('{% url 'app01:getpwd' %}', $('form').serialize(), function (data) {

            if (data['s'] == 1) {
                    layer.msg(data['info']);
                    // 更新密码页面
                    var html1 = "新密码:<input type='text' name='pwd'><br>确认密码:<input type='text' name='repwd'> <input type='button' value='更改密码'> ";
                    $(".maina").html(html1);
                } else {
                    //登陆失败
                    layer.msg(data['info']);
                }
                return false;
                },'json');
    });

    $(document).on('click','#repwd',function () {
        $.post('{% url 'app01:repwd' %}', $('form').serialize(), function (data) {

            if (data['s'] == 1) {
                    layer.msg(data['info']);
                // 跳转主页面
                    location.href="{% url 'app01:index' %}";
                } else {
                    //更改失败
                    layer.msg(data['info']);
                }
                return false;
                },'json');
    });
</script>

 

posted @ 2019-08-08 19:23  一觉昏睡人  阅读(282)  评论(0编辑  收藏  举报