• 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>
  • 相关阅读:
    上传图片并实现本地预览
    a标签传递参数
    HTTP错误 404.17–Not Found 请求的内容似乎是脚本,因而将无法有静态文件处理程序来处理
    VM虚拟机无法拖拽、粘贴、复制
    ORA-01461: 仅能绑定要插入 LONG 列的 LONG 值
    Oracle中Clob类型处理解析:ORA-01461:仅可以插入LONG列的LONG值赋值
    Oracle获取表结构信息:表名、是否视图、字段名、类型、长度、非空、主键
    SQLServer2005,2000获取表结构:字段名、类型、长度、主键、非空、注释
    c# float和double的“坑”
    VS活动解决方案平台
  • 原文地址:https://www.cnblogs.com/person1-0-1/p/11323231.html
Copyright © 2020-2023  润新知