验证码,要导入两个模块,check_code.py 和 Monaco.py(字体文件)文件,
链接:https://pan.baidu.com/s/1neiX2JBwoe2oN_Ng8J8UJA 密码:y08d
两个模块我都放在 utils 文件夹下,导入就能用了
下面这个是生成验证码的视图函数。
# views.py 函数
from io import BytesIO
from django.shortcuts import HttpResponse
from django.shortcuts import render
from utils.check_code import create_validate_code
def test(request):
if request.method == "GET":
return render(request,"test.html")
elif request.method == 'POST':
input_code = request.POST.get("check_code")
check_cd = request.session["check_code"]
print(input_code,check_cd)
return HttpResponse('hahaha')
def check_code(request):
f = BytesIO() // 这个是存到内存中,
img,code = create_validate_code()
img.save(f,"PNG")
print(code)
request.session["check_code"] = code // 验证码要保存到session中,才方便验证
return HttpResponse(f.getvalue()) // 从内存中读出数据
模版文件
# test.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/test.html/" method="post">
<p>用户名:<input type="text" placeholder="用户名" name="username"></p>
<p>密码:<input type="password" placeholder="密码" name="password"></p>
<p>验证码:<input type="text" placeholder="验证码" name="check_code">
<img src="/check_code.html" alt="" onclick="changeImg(this)">
</p>
<p><input type="submit" value="提交"></p>
</form>
<script>
function changeImg(that) {
that.src = that.src + '?';
// 添加?之后img就会重新请求一次,不过路径后的?号会一直增加
// 效果是点击验证码图片会自动更新
}
</script>
</body>
</html>
urls.py
url(r"^test.html$", account.test),
url(r"^check_code_my.html$", account.check_code_my),
1.png
设置一个月免登录
# 直接对 request 设置
request.session.set_expiry(60 * 60 * 24 * 30) // 30天免登录
或者可以在 setting 文件修改,不过这个是对所有 client 生效的
SESSION_COOKIE_AGE = 1209600
# 1209600 = 60*60*24*14 十四天
note
补充: 验证码
1.点击更换验证码
2.验证码到底是否有必要是图片?有
3.
服务端:
a. session中保存随机验证码,如:87fs
b. 在页面中显示图片
客户端:
a. 显示图片
b. 在cookie中保存sessionid
验证码图片
验证码字符串:87fs
sessionid='asdfasdfasdfasdfasdfasdf09'
{
"asdfasdfasdfasdfasdfasdf09": {'ck': 87fs}
"ffsdfsdfsdfsdfsqq243234234": {'ck': 98fd}
"ffsdfsdfsdfsdfsqq243234234": {'ck': 98fd}
"ffsdfsdfsdfsdfsqq243234234": {'ck': 98fd}
"ffsdfsdfsdfsdfsqq243234234": {'ck': 98fd}
"ffsdfsdfsdfsdfsqq243234234": {'ck': 98fd}
"ffsdfsdfsdfsdfsqq243234234": {'ck': 98fd}
"ffsdfsdfsdfsdfsqq243234234": {'ck': 98fd}
}
ffsdfsdfsdfsdfsqq243234234 : 9999
使用:
check_code.py
Monaco.ttf
<img src="/shizhengwen.html" onclick="changeImg(this);">
<script>
function changeImg(ths) {
ths.src = ths.src + "?";
}
</script>
def xiaoyun(request):
if request.method == "GET":
return render(request,'xiaoyun.html')
else:
input_code = request.POST.get('code')
check_cd = request.session['check_code']
print(input_code,check_cd)
return HttpResponse('...')
def shizhengwen(request):
"""生成验证码图片"""
# f = open('static/imgs/avatar/20130809170025.png','rb')
# data = f.read()
# f.close()
f = BytesIO()
img, code = create_validate_code()
request.session['check_code'] = code
img.save(f, 'PNG')
# request.session['CheckCode'] = code
return HttpResponse(f.getvalue())
补充: 一个月免登陆
request.session.set_expiry(60 * 60 * 24 * 30)
SESSION_COOKIE_AGE = 1209600
网友评论