本文实例讲述了django框架验证码用法。分享给大家供大家参考,具体如下:
验证码
1、作用
- 在用户登录,注册以及一些敏感操作的时候,我们为了防止服务器被暴力请求,或爬虫爬取,我们可以使用验证码进行过滤,减轻服务器的压力。
-
验证码需要使用绘图 pillow
- pip3 install pillow
- 核心api
-
image
- 需要模式
- 尺寸
- 背景色
-
imagedraw
- 绑定画布
- 模式
- 封装了绘制的api
- text
- point
- line
- arch
-
imagefont
- 手动指定字体
2、业务流程
绘制验证码图片
1
|
background = ( 10 , 20 , 30 ) / / rgb颜色
|
初始化画布
1
|
image = image.new(‘rgb',( 100 , 50 ),background)
|
获取画布中画笔对象
1
|
draw = imagedraw.draw(image)
|
绘制验证码,随机四个
1
2
3
|
font = imagefont.truetype(‘path',size)
fontcolor = ( 20 , 40 , 60 )
draw.text((x,y), 'r' ,font,fontcolor)
|
返回验证码内容
1
2
3
4
5
6
7
8
|
# 删除画笔
del draw
#保存图片到bytesio对象
import io
buf = io.bytesio()
image.save(buf, 'png' )
#返回bytesio中的内容
return httpresponse(buf.getvalue(), 'image/png' )
|
3、代码范例
html页面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<form method = "post" action = "{% url 'sitesapp:login' %}" >
{ % csrf_token % }
<div class = "login" >
<div class = "input-group" >
<span class = "input-group-addon" id = "basic-addon1" >用户名< / span>
< input type = "text" class = "form-control" placeholder = "username" aria - describedby = "basic-addon1" name = "uname" >
< / div>
<div class = "input-group" >
<span class = "input-group-addon" id = "basic-addon1" >密 码< / span>
< input type = "text" class = "form-control" placeholder = "password" aria - describedby = "basic-addon1" name = "upswd" >
< / div>
<div class = "input-group" >
<span class = "input-group-addon" id = "basic-addon1" >验证码< / span>
< input type = "text" class = "form-control" placeholder = "auth code" aria - describedby = "basic-addon1" name = "ucode" >
< / div>
<div class = "vcode" >
<img src = "/app/getvcode/" id = "vcode" >
< / div>
< input type = "submit" class = "loginbtn" value = "登 录" ><br>
< / div>
< / form>
<script type = "text/javascript" >
$(function () {
$( '#vcode' ).click(function () {
$(this).attr( 'src' , "/app/getvcode" + math.random())
})
})
< / script>
|
views视图
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
'''
生成并返回验证码
'''
def getvcode(request):
# 随机生成验证码
population = string.ascii_letters + string.digits
letterlist = random.sample(population, 4 )
vcode = ''.join(letterlist)
# 保存该用户的验证码
request.session[ 'vcode' ] = vcode
# 绘制验证码
# 需要画布,长宽颜色
image = image.new( 'rgb' ,( 176 , 60 ),color = getrandomcolor())
# 创建画布的画笔
draw = imagedraw.draw(image)
# 绘制文字,字体所在位置
path = os.path.join(base_dir, 'static' , 'fonts' , 'adobearabic-bolditalic.otf' )
font = imagefont.truetype(path, 50 )
for i in range ( len (vcode)):
draw.text(( 20 + 40 * i, 0 ),vcode[i],fill = getrandomcolor(),font = font)
# 添加噪声
for i in range ( 500 ):
position = (random.randint( 0 , 176 ),random.randint( 0 , 50 ))
draw.point(position,fill = getrandomcolor())
# 返回验证码字节数据
# 创建字节容器
buffer = io.bytesio()
# 将画布内容丢入容器
image.save( buffer , 'png' )
# 返回容器内的字节
return httpresponse( buffer .getvalue(), 'image/png' )
# 获取随机颜色
def getrandomcolor():
red = random.randint( 0 , 255 )
green = random.randint( 0 , 255 )
blue = random.randint( 0 , 255 )
return (red,green,blue)
|
希望本文所述对大家基于django框架的python程序设计有所帮助。
原文链接:https://blog.csdn.net/lm_is_dc/article/details/80527830