Implementation of sending verification code by mailbox in django registration
- 2021-11-01 03:40:03
- OfStack
View code
lis = []# Settings 1 An empty list is used to store the sent verification code for verification
def yzm1():
res1 = ""
for i in range(4):# Use 4 Verification code composed of random numbers
num = random.randint(0, 9)
res1 += str(num)
lis.append(res1)# Put the authentication code in an empty list
return res1# Return verification code
class zc(View):
def get(self, request):
return render(request, 'zc.html', locals())
def post(self, request):
name = request.POST['qq']# Get the data passed in by the front end
subject = ' Verification code '
message = vercode()# Get verification code
form = '837620306@qq.com'# Get the mailbox that sent the verification code
# EMAIL_HOST_USER
rcipient_list = [name]
res = send_mail(subject=subject, message=message, from_email=form, recipient_list=rcipient_list)# Send verification code to return goods true or false, false means that it was not sent successfully
if res == 1:# Determine whether the transmission is successful
return redirect('/app01/yzm/')# If successful, jump to the verified web page
else:# If it is unsuccessful, return to the registration interface
return render(request, 'zc.html', locals())
def yzm2(request):
if request.method == 'GET':
return render(request, 'yzm.html', locals())
elif request.method == 'POST':
a = request.POST['much']# Get the verification code entered by the user
if a == lis[-1]:# Take the latest verification code
return HttpResponse(' Verification code is correct ')
else:
return HttpResponse('no')
The mailbox used to send the verification code needs to open the POP3/SMTP service and the IMAP/SMTP service and obtain the authorization code for the POP3/SMTP service
settings Configuration File
In addition, you need to set the settings file of django
# Set up the outgoing mail server: smtp.qq.com ,
EMAIL_HOST = 'smtp.qq.com'
# Set the port number, if you are using SSL The port number is 465 Or 587
EMAIL_PORT = 25
# Set up the sender mailbox
EMAIL_HOST_USER = 'xxxxx'
# Set Sender Authorization code (POP3/SMTP Authorization code of service )
EMAIL_HOST_PASSWORD = 'xxxxx'
# Set whether secure links are enabled
EMAIL_USER_TLS = True