只展示了views页面中的代码
登陆界面
def login(request):
return render(request,‘login.html’)
自定义颜色
def make_color():
red = random.randrange(255)
blue = random.randrange(255)
green = random.randrange(255)
return (red,blue,green)
自定义模糊背景(这里为色点)
def make_point():
hight = random.randrange(100)
weight = random.randrange(50)
return (hight,weight)
def huahua(request,):
size = (100,50)
# 创建画布
image = Image.new('RGB',size,color = make_color())
# 创建画笔
draw = ImageDraw.Draw(image,'RGB')
# 创建字体
font = ImageFont.truetype('/home/xu/桌面/pystudy/day07_pm/static/uploadefile/fonts/ADOBEARABIC-BOLD.OTF',size=32)自己创建的字体库路径
# 创建数据原
source = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890'自己想要生成的验证码内容
# 生成验证码
yzm = ''
for i in range(4):
yzm += source[random.randrange(len(source))]
# 配色
for item in range(4):
draw.text((20 + item * 20, 10), yzm[item], fill=make_color(), font=font)
# 模糊背景
for a in range(600):
draw.point(make_point(),fill=make_color())
#保存
bytes_io = io.BytesIO()
image.save(bytes_io,'png')
# 重定向
response = HttpResponse(bytes_io.getvalue())
response.set_cookie('yzm',yzm,max_age=30)
return response
将输入的验证码与保存到coookie中的验证码进行比对
def dologin(request):
yzm = request.POST.get('yzm')
c_yzm = request.COOKIES.get('yzm')
if yzm == c_yzm:
return HttpResponse('登陆成功')
return HttpResponse('验证码错误')
这只是作为自己学习的基础练习项目