Django: use email to get verification code

The verification code is used to determine whether the current user is matched with the user to be verified, and plays an important role in scenarios that require identity verification such as registration and password modification.
Simple verification methods include receiving verification codes on mobile phones and receiving verification codes via email.
Here will mainly implement QQ mailbox verification.

1. Turn on the third-party mail sending service

Log in to the QQ mailbox web version, click on the top "Settings" >> "Accounts" >> Open POP3/IMAP/SMTP service >> Generate authorization code ( please keep it confidential )

2. Mailbox configuration

EMAIL_USE_SSL = True
EMAIL_HOST = ‘smtp.qq.com’
EMAIL_PORT = 465
EMAIL_HOST_USER = ‘[email protected]
EMAIL_HOST_PASSWORD = ‘授权码’
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER

3. Template file

<!DOCTYPE html>
<html>
<head>
	{% load static %}
	<title>找回密码</title>
	<link rel="stylesheet" href="{% static "css/reset.css" %}" />
	<link rel="stylesheet" href="{% static "css/user.css" %}" />
    <script src="{% static "js/jquery.min.js" %}"></script>
    <script src="{% static "js/user.js" %}"></script>
</head>
<body>
<div class="page">
	<div class="loginwarrp">
		<div class="logo">找回密码</div>
        <div class="login_form">
			<form id="Login" name="Login" method="post" action="">
                {% csrf_token %}
				<li class="login-item">
					<span>用户名:</span>
					<input type="text" name="username" class="login_input">
                    <span id="count-msg" class="error"></span>
				</li>
                {% if password %}
				<li class="login-item">
					<span>新密码:</span>
					<input type="password" name="password" class="login_input">
                    <span id="password-msg" class="error"></span>
				</li>
                {% endif %}
                {% if VCodeInfo %}
                    <li class="login-item">
                        <span>验证码:</span>
                        <input type="text" name="VCode" class="login_input">
                        <span id="password-msg" class="error"></span>
				    </li>
                {% endif %}
                <div>{
   
   { tips }}</div>
				<li class="login-sub">
					<input type="submit" name="Submit" value="{
     
     { button }}">
				</li>
           </form>
		</div>
	</div>
</div>
<script type="text/javascript">
	window.onload = function() {
     
     
		var config = {
     
     
			vx : 4,
			vy : 4,
			height : 2,
			width : 2,
			count : 100,
			color : "121, 162, 185",
			stroke : "100, 200, 180",
			dist : 6000,
			e_dist : 20000,
			max_conn : 10
		};
		CanvasParticle(config);
	}
</script>
<script src="{% static "js/canvas-particle.js" %}"></script>
</body>
</html>

Insert picture description here

4. View function

def findpsView(request):
    button = '获取验证码'	# 模板上下文:按钮提示内容
    VCodeInfo = False 	# 模板上下文:是否已发送验证码
    password = False	# 模板上下文:是否生成密码输入框
    if request.method == 'POST':
        u = request.POST.get('username')
        p = request.POST.get('password')
        VCode = request.POST.get('VCode', '') # 模板上下文:产生的验证码
        user = User.objects.filter(username=u)
        # 用户不存在
        if not user:
            tips = '用户' + u + '不存在'
        else:
            # 判断验证码是否已发送
            if not request.session.get('VCode', ''):
                # 发送验证码并将验证码写入session
                button = '重置密码'
                tips = '验证码已发送'
                password = True
                VCodeInfo = True
                VCode = str(random.randint(000000, 999999))
                request.session['VCode'] = VCode
                user[0].email_user('找回密码', VCode)
            # 匹配输入的验证码是否正确
            elif VCode == request.session.get('VCode'):
                # 密码加密处理并保存到数据库
                dj_ps = make_password(p, None, 'pbkdf2_sha256')
                user[0].password = dj_ps
                user[0].save()
                del request.session['VCode']
                tips = '密码已重置'
            # 输入验证码错误
            else:
                tips = '验证码错误,请重新获取'
                VCodeInfo = False
                password = False
                del request.session['VCode']
    return render(request, 'user.html', locals())

Just refill the password when you receive the email.
Insert picture description here

Guess you like

Origin blog.csdn.net/dangfulin/article/details/107557465