利⽤python⽣成图形验证码validCode.py
import random
from io import BytesIO
from PIL import Image, ImageDraw, ImageFont
def get_random_color():
return (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
def get_valid_code_img(request):
img = w('RGB', (260, 34), color=get_random_color())
draw = ImageDraw.Draw(img)
kumo_font = uetype('static/f', size=28)
valid_code_str = ''
for i in range(5):
random_num = str(random.randint(0, 9))
random_low_alpha = chr(random.randint(97, 122))
random_high_alpha = chr(random.randint(65, 90))
random_char = random.choice([random_num, random_low_alpha, random_high_alpha])
<((i * 50 + 20, 5), random_char, get_random_color(), font=kumo_font)
# 保存验证码字符串
valid_code_str += random_char
# 噪点噪线
width = 260
height = 34
for i in range(5):
x1 = random.randint(0, width)
x2 = random.randint(0, width)
y1 = random.randint(0, height)
y2 = random.randint(0, height)
draw.line((x1, y1, x2, y2), fill=get_random_color())
for i in range(5):
draw.point([random.randint(0, width), random.randint(0, height)], fill=get_random_color())
x = random.randint(0, width)
y = random.randint(0, height)
draw.arc((x, y, x + 4, y + 4), 0, 90, fill=get_random_color())
# 保存验证码字符串到该⽤户的ssion
request.ssion['valid_code_str'] = valid_code_str
f = BytesIO() # ⽤完之后,BytesIO会⾃动清掉
img.save(f, 'png')
data = f.getvalue()
return data
views.py
ib import auth
from django.shortcuts import render, HttpRespon
from django.http import JsonRespon
from blog.utils.validCode import get_valid_code_img
def get_validCode_img(request):
"""
基于PIL模块动态⽣成响应状态码图⽚
:param request:
:return:
"""
data = get_valid_code_img(request)
return HttpRespon(data)
def login(request):
hod == 'POST':
respon = {'ur': None, 'msg': None}
ur = ('ur')
pwd = ('pwd')
valid_code = ('valid_code')
valid_code_str = ('valid_code_str')
if valid_code.lower() == valid_code_str.lower():
ur = auth.authenticate(urname=ur, password=pwd)
if ur:
auth.login(request, ur) # request.ur == 当前登录对象
respon['ur'] = ur.urname
el:
respon['msg'] = '⽤户名或密码错误'
el:
respon['msg'] = '验证码错误'
return JsonRespon(respon)
return render(request, 'login.html')
login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta chart="UTF-8">
<title>登陆页⾯</title>
<link rel="stylesheet" href="/static/blog/bs/css/bootstrap.css">
</head>
<body>
<h3>登陆页⾯</h3>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offt-3">
<form id="fm">
{% csrf_token %}
<div class="form-group">
<label for="id_ur">⽤户名</label>
<input type="text" name="ur" id="id_ur" class="form-control">
</div>
<div class="form-group">
<label for="id_pwd">密码</label>
<input type="text" name="pwd" id="id_pwd" class="form-control">
</div>
<div class="form-group">
<label for="id_valid_code">验证码</label>
<div class="row">
<div class="col-md-6">
<input type="text" name="valid_code" id="id_valid_code" class="form-control">
</div>
<div class="col-md-6">
<img width="260" height="34" src="/get_validCode_img/" id="valid_code_img">
</div>
</div>
</div>
<input type="button" value="提交" id="login_btn" class="btn btn-default ">
<span id="error"></span>
</form>
</div>
</div>
</div>
<script src="/static/blog/js/jquery-3.3.1.js"></script>
<script>
// 验证码图⽚刷新
$('#valid_code_img').click(function () {
$(this)[0].src += '?'; // 问号的唯⼀意义是:图⽚链接发⽣了变化,图⽚需要刷新
});
// 登陆验证
$('#login_btn').click(function () {
$.ajax({
url: '',
type: 'post',
data: $('#fm').rialize(),
success: function (data) {
if (data.ur) {
location.href = '/index/'
} el {
$('#error').text(data.msg).css({'color': 'red', 'margin-left': '10px'});
tTimeout(function () {
$('#error').text('') // 3秒后清空error信息 }, 3000)
}
}
})
})
</script>
</body>
</html>