Generate random verification code in the background
#Verification Code Generation-in the login.html, there is a verification code picture set the src URL of the picture, the equivalent of sending a GET request, the content returned is the content of the picture rendered<divclass="col-sm-5"> <input type="Password" class="Form-control"Id="InputPassword3"Placeholder="Verification Code"> </div> <divclass="col-sm-5"> "/check_code/"Width="150px"height="34px"title="Click Refresh Verification Code"> </div>-randomly generate picture contentdefCheck_code (Request): fromPILImportImage,imagedraw fromIoImportBytesio F= Bytesio ()#open up a space from memoryimg = image.new (mode="RGB", Size= (150,34), color= (255, 255, 255)) Draw= Imagedraw.draw (IMG, mode='RGB') #set font for picture text fromPILImportImagefont Font= Imagefont.truetype ("Kumo.ttf", 25) #7 Adding content to a picture set a different color for each character ImportRandom code_list= [] forIinchRange (5): Char= Chr (Random.randint (65,90) ) code_list.append (char) draw.text ((I*20,10), char, fill= (Random.randint (0,255), Random.randint (0,255), Random.randint (0,255)), Font=font)#Add content EndImg.save (F,'PNG')#Save picture contents to memorydata = F.getvalue ()#Read picture content returnHttpResponse (data)
View Code
The verification code is saved in session, the user submits the verification code and the session verification code to compare, completes the Verification Code verification
-Save the verification code in session to remember the user. So that the code is validated by the following verification code="". Join (code_list) request.session["Code"] =Code-Login Verification Code VerificationdefLogin (Request):ifRequest.method = ='GET': returnRender (Request,'login.html') Else: Input_code= Request. Post.get ("Code") Session_code= Request.session.get ('Code') ifInput_code.upper () = =session_code.upper ():returnHttpResponse ("code is correct") Else: returnHttpResponse ("Code is invalid")
View Code
Encapsulating the Verification code
-Verification Code EncapsulationImportRandom fromPILImport(Image,imagedraw,imagefont,imagefilter)defCheck_code (width=120, height=30, char_length=5, font_file='Kumo.ttf', font_size=28): Code=[] img= Image.new (mode='RGB', size= (width, height), color= (255, 255, 255)) Draw= Imagedraw.draw (IMG, mode='RGB') defRndchar ():"""generate random letters: return:""" returnChr (Random.randint (65, 90)) defRndcolor ():"""generate random color: return:""" return(Random.randint (0, 255), Random.randint (ten, 255), Random.randint (64, 255)) #Write TextFont =Imagefont.truetype (Font_file, font_size) forIinchRange (char_length): Char=Rndchar () code.append (char) H= Random.randint (0, 4) Draw.text ([I* Width/char_length, H], char, Font=font, fill=Rndcolor ())#Write interference points forIinchRange (40): Draw.point ([Random.randint (0, width), random.randint (0, height)], fill=Rndcolor ())#Write interference circles forIinchRange (40): Draw.point ([Random.randint (0, width), random.randint (0, height)], fill=Rndcolor ()) x=random.randint (0, width) y=random.randint (0, height) draw.arc ((x, y, x+ 4, Y + 4), 0, fill=Rndcolor ())#Draw Interference Lines forIinchRange (5): X1=random.randint (0, width) y1=random.randint (0, height) x2=random.randint (0, width) y2=random.randint (0, height) draw.line ((x1, y1, x2, y2), fill=rndcolor ()) img=Img.filter (Imagefilter.edge_enhance_more)returnImg"'. Join (Code)
View Code
Get Verification Code Background view
#post-encapsulation use defCheckout (Request): fromIoImportBytesio fromUtils.checkcodeImportcheck_code img, code=Check_code () stream=Bytesio () Img.save (stream,'PNG') request.session['Code'] =CodereturnHttpResponse (Stream.getvalue ())
View Code
[Oldboy-django] [2 in-depth Django] Login Registration page Verification Code