SoFunction
Updated on 2024-11-19

Implementation of django registration with email to send verification code

view code

lis = []# Set up an empty list to hold sent captchas for verification
def yzm1():
    res1 = ""
    for i in range(4):#Captcha with four random numbers
        num = (0, 9)
        res1 += str(num)
    (res1)#Put CAPTCHA into an empty list
    return res1#Return to CAPTCHA
class zc(View):
    def get(self, request):
        return render(request, '', locals())
    def post(self, request):
        name = ['qq']# Get the data passed in from the front end
        subject = 'CAPTCHA'
        message = vercode()#GetCAPTCHA
        form = '837620306@'# Get the email to which the captcha was sent
        # EMAIL_HOST_USER
        rcipient_list = [name]
        res = send_mail(subject=subject, message=message, from_email=form, recipient_list=rcipient_list)#Send CAPTCHA return goods true or false, false means not sent successfully
        if res == 1:# Determine if the send was successful
            return redirect('/app01/yzm/')#If successful, you will be redirected to the verification page
        else:# Return to the registration screen if unsuccessful
            return render(request, '', locals())
def yzm2(request):
    if  == 'GET':
        return render(request, '', locals())
    elif  == 'POST':
        a = ['much']# Get the captcha entered by the user
        if a == lis[-1]:# Fetch the latest CAPTCHA
            return HttpResponse('Captcha is correct')
        else:
            return HttpResponse('no')

The mailbox used to send CAPTCHA needs to open POP3/SMTP service and IMAP/SMTP service, and get the authorization code of POP3/SMTP service.

settings configuration file

Another thing you need to do is to set up the django settings file

# Set up the sending mail server:.
EMAIL_HOST = ''
# Set the port number, 465 or 587 if SSL is being used.
EMAIL_PORT = 25
#Set the sender's mailbox
EMAIL_HOST_USER = 'xxxxx'
# Set sender authorization code (authorization code for POP3/SMTP service)
EMAIL_HOST_PASSWORD = 'xxxxx'
# Set whether to enable secure links
EMAIL_USER_TLS = True

To this point this article on django registration with mailboxes to send a verification code to the realization of the article is introduced to this, more related to django mailboxes verification code content, please search for my previous articles or continue to browse the following related articles I hope that you will support me more in the future!