首先我们网上找一个生成验证码的程序:
from PIL import Image, ImageFont, ImageDraw, ImageFilter
import random
# 返回随机字母
def charRandom():
return chr((random.randint(65, 90)))
# 返回随机数字
def numRandom():
return random.randint(0, 9)
# 随机颜色
def colorRandom1():
return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255))
# 随机长生颜色2
def colorRandom2():
return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127))
width = 60 * 4
height = 60
image = Image.new('RGB', (width, height), (255, 255, 255));
# 创建font对象
font = ImageFont.truetype('C:/Windows/Fonts/Calibri.ttf', 36);
# 创建draw对象
draw = ImageDraw.Draw(image)
# 填充每一个颜色
for x in range(width):
for y in range(height):
draw.point((x, y), fill=colorRandom1())
# 输出文字
for t in range(4):
draw.text((60 * t + 10, 10), charRandom(), font=font, fill=colorRandom2())
# 模糊
image = image.filter(ImageFilter.BLUR)
image.save('code.jpg', 'jpeg')
1.在views.py文件下创建一个生成验证码的程序
from PIL import Image, ImageFont, ImageDraw, ImageFilter
import random
import io
def yanzhengmashengcheng(request):
# 返回随机数字
# def numRandom():
# return random.randint(0, 9)
# 随机颜色
# def colorRandom1():
# return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255))
# 随机长生颜色2
# def colorRandom2():
# return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127))
width = 60 * 4
height = 60
image = Image.new('RGB', (width, height), (255, 255, 255))
# 创建font对象
font = ImageFont.truetype('/home/python/Downloads/pycharm-2017.3.4/jre64/lib/fonts/ARIAL.TTF', 36)
# 创建draw对象
draw = ImageDraw.Draw(image)
# 填充每一个颜色
for x in range(width):
for y in range(height):
draw.point((x, y), fill=(random.randint(64, 255), random.randint(64, 255), random.randint(64, 255)))
b=''
# 输出文字
for t in range(4):
a=chr((random.randint(65, 90)))
b+=a
draw.text((60 * t + 10, 10), a, font=font, fill=(random.randint(32, 127), random.randint(32, 127), random.randint(32, 127)))
# 模糊
request.session['password'] = b
request.session.set_expiry(3600)
image = image.filter(ImageFilter.BLUR)
buf = io.BytesIO()
# 将图片保存在内存中,文件类型为png
image.save(buf, 'png')
# 将内存中的图片数据返回给客户端,MIME类型为图片png
return HttpResponse(buf.getvalue(), 'image/png')
2.在templates创建一个yanzhengma.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
function refresh_check_code(ths) {
ths.src += '?';
{#src后面加问好会自动刷新验证码img的src#}
}
</script>
</head>
<body>
<img src="/yanzhengmashengcheng" onclick="refresh_check_code(this)">
<form method="get" action="/yanzhengmayanzheng" class="yanzhengma">
<ul>
<li>
<input type="text" name="password" >
</li>
<li>
<input type="submit" value="提交" class="sub">
</li>
</ul>
</form>
</body>
</html>
3.在views.py创建一个关联网页的函数
def yanzhengma(request):
return render(request,'yanzhengma.html')
4.在settings.py的同级文件urls.py下的urlpatterns列表中加入url(r’^’,include(‘user.urls’))
这里user是我自己创的一个个app,viwes.py和urls.py都在这个app内
5.在user文件下的urls中导入views:
from django.conf.urls import include, url
from user import views
并在urlpatterns列表中加入
url(r'^yanzhengmashengcheng$',views.yanzhengmashengcheng),
url(r'^yanzhengmayanzheng$',views.yanzhengmayanzheng),
url(r'^yanzhengma$',views.yanzhengma)
6.在views.py文件中写验证函数:
def yanzhengmayanzheng(request):
if request.method == 'GET':
parm = request.GET
print(parm)
password_get= parm.get('password')
print(password_get)
try:
a=request.session.get('password','错误')
if a :
# request.session.flush()
return HttpResponse('ok')
except:
return HttpResponse('false')
这时打开站点进行验证