1.验证码插件
验证码插件django-simple-captcha
安装后再settings配置
INSTALLED_APPS = [
# 添加验证码插件
'captcha'
]
执行数据库迁移,生成一个表用于存储生成过的验证码,用于验证
配置主路由
# 所有captcha开头的路由都验证,自带captcha.urls文件
path('captcha/', include('captcha.urls')),
在想要加入验证码的类或方法里加入captcha = CaptchaField()
在view方法里根据前端传来的验证码在数据库查找
captcha_obj = CaptchaStore.objects.get(hashkey=key)
2.自定义验证码
# 自定义生成验证码
def get_color():
red = random.randint(0,256)
green = random.randint(0, 256)
blue = random.randint(0, 256)
return red, green, blue
def get_code():
s='0123456789qwertyuiopasdfghjklzxcvbnmZMXNCBVLASKDJFHGQPOWIERUYT'
code = ''
for i in range(4):
code += random.choice(s)
return code
def draw(request):
# 指定画布长宽
width = 120
height = 40
image_size = (width,height)
# 定义画布
image = Image.new('RGB', image_size, get_color())
# 定义画笔
draw = ImageDraw.Draw(image)
# 绘制干扰线
for i in range(10):
# 指定起始结束位置
begin = (random.randint(0,width), random.randint(0,height))
end = (random.randint(0, width), random.randint(0, height))
# 使用画笔绘制,并定义样式颜色等
draw.line((begin,end), fill=(get_color()))
# 绘制干扰点
for i in range(20):
draw.point((random.randint(0,width), random.randint(0,height)),fill=(get_color()))
# 产生验证码
code = get_code()
# 指定字体和大小
myfont = ImageFont.truetype(font='static/fonts/arial.ttf', size=30)
# 逐个绘制字符
for i in range(4):
# 每绘制一个,x周位置改变,y可以不变
distance_x = random.randint(30*i,30*i+5)
distance_y = random.randint(0,5)
draw.text((distance_x,distance_y),code[i],font=myfont,fill=(get_color()))
# 滤镜边界加强
image= image.filter(ImageFilter.EDGE_ENHANCE_MORE)
# 定义一个缓存
buffer = BytesIO()
# 将图片放到缓存
image.save(buffer,'JPEG')
# 获取缓存内容
buf_bytes = buffer.getvalue()
# 将code保存到session中用于验证
request.session['code'] = code
return HttpResponse(buf_bytes,'image/jpeg')
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户登录</title>
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
</head>
<body>
<form action="{% url 'myapp:login1' %}" method="post">
{% csrf_token %}
{{ user_login_form.as_p }}
<p><label for="">验证码:</label><input type="text" name="code"><img id="generated_code" src="{% url 'myapp:draw' %}" alt=""></p>
<p><span id="result">{{ msg }}</span></p>
<br>
<input type="submit" value="登录">
</form>
</body>
<script>
$('#generated_code').click(function() {
path = "{% url 'myapp:draw' %}?ran="+Math.random()
$(this).attr('src',path)
})
</script>
</html>