• django项目中使用邮箱找回密码功能


    本文使用qq邮箱,需要登录邮箱,在设置-账户里面开启SMTP服务,要记下授权码

    前端html

    {#找回密码的表单#}
    
    <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结束  #}

    路由

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

    方法

    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文件,封装生成的随机密码

    # 随机数
    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文件中设置

    EMAIL_HOST='smtp.qq.com'
    EMAIL_PORT=465
    EMAIL_HOST_USER='3028981710@qq.com'#账号
    EMAIL_HOST_PASSWORD='kimtvyyymbwodgaf'#密码(授权码)
    EMAIL_USE_SSL=True

    页面效果

    done。

  • 相关阅读:
    OneProxy与其它数据库中间件的对比
    防御式编程
    google jam 比赛题(设计有问题)
    Python 代码性能优化技巧
    Python性能鸡汤
    如何避免重构带来的危险
    Linux/Unix工具与正则表达式的POSIX规范
    代码抽象层次2
    chinaunix:腾讯面试题
    C++异常处理小例
  • 原文地址:https://www.cnblogs.com/nmsghgnv/p/11366130.html
Copyright © 2020-2023  润新知