制作验证码的函数
# 制作生成验证码的函数 import random from PIL import Image,ImageDraw,ImageFont,ImageFilter def check_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') def rndChar(): """ 生成随机字母 :return: """ return chr(random.randint(65, 90)) def rndColor(): """ 生成随机颜色 :return: """ return (random.randint(0, 255), random.randint(10, 255), random.randint(64, 255)) # 写文字 font = ImageFont.truetype(font_file, font_size) for i in range(char_length): char = rndChar() code.append(char) h = random.randint(0, 4) draw.text([i * width / char_length, h], char, font=font, fill=rndColor()) # 写干扰点 for i in range(40): draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor()) # 写干扰圆圈 for i in range(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, 90, fill=rndColor()) # 画干扰线 for i in range(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) return img, ''.join(code)
整体文件
# url.py
from django.contrib import admin from django.urls import path from my_qpp import views urlpatterns = [ path('admin/', admin.site.urls), path('login/', views.login), path('code/', views.code), ]
# views.py
from django.shortcuts import render,HttpResponse,redirect # 制作生成验证码的函数 import random from PIL import Image,ImageDraw,ImageFont,ImageFilter def check_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') def rndChar(): """ 生成随机字母 :return: """ return chr(random.randint(65, 90)) def rndColor(): """ 生成随机颜色 :return: """ return (random.randint(0, 255), random.randint(10, 255), random.randint(64, 255)) # 写文字 font = ImageFont.truetype(font_file, font_size) for i in range(char_length): char = rndChar() code.append(char) h = random.randint(0, 4) draw.text([i * width / char_length, h], char, font=font, fill=rndColor()) # 写干扰点 for i in range(40): draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor()) # 写干扰圆圈 for i in range(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, 90, fill=rndColor()) # 画干扰线 for i in range(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) return img, ''.join(code) def code(request): """ 生成图片验证码 :param request: :return: """ img,random_code = check_code() request.session['random_code'] = random_code from io import BytesIO stream = BytesIO() img.save(stream, 'png') return HttpResponse(stream.getvalue()) def login(request): """ 用户登陆 :param request: :return: """ if request.method == 'GET': return render(request,'login.html') user = request.POST.get('user') pwd = request.POST.get('pwd') code = request.POST.get('code') if code.upper() != request.session['random_code'].upper(): return render(request,'login.html',{'msg':'验证码错误'}) if user == 'dujufei' and pwd == '123': return redirect('http://www.py3study.com') return render(request, 'login.html', {'msg': '用户名或密码错误'})
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> {# <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet">#} </head> <body> <form method="post"> {% csrf_token %} <p> <input type="text" name="user" placeholder="用户名" /> </p> <p> <input type="password" name="pwd" placeholder="密码" /> </p> <p> <input type="text" name="code" placeholder="验证" /> <img onclick="changeImg(this);" src="/code/" alt="" title="点击更换"> </p> <input type="submit" value="提交">{{ msg }} </form> <script> function changeImg(ths) { ths.src = ths.src + '?'; } </script> </body> </html>
还有一个字体文件,kumo.ttf,将字体文件放到与manage.py同名的目录下即可,下载地址:
https://github.com/py3study/cnblog/blob/master/kumo.ttf