Django实现验证码
来源:互联网 发布:单片机各引脚功能 编辑:程序博客网 时间:2024/06/15 19:19
和php的思路差不多,先用绘图的库生成验证码,然后通过session保存,最后post提交验证。
目录树
.
├── db.sqlite3
├── manage.py
├── SchoolBuy
│ ├── init.py
│ ├── settings.py
│ ├── urls.py
│ ├── utils
│ │ ├── check_code.py
│ ├── views.py
│ └── wsgi.py
└── templates
└── test_code.html
用PIL生成验证码
check_code.py
import random,stringfrom PIL import Image,ImageDraw,ImageFont,ImageFilter#生成随机字符串def getRandomChar(): #string模块包含各种字符串,以下为小写字母加数字 ran = string.ascii_lowercase+string.digits char = '' for i in range(4): char += random.choice(ran) return char#返回一个随机的RGB颜色def getRandomColor(): return (random.randint(50,150),random.randint(50,150),random.randint(50,150))def create_code(): #创建图片,模式,大小,背景色 img = Image.new('RGB', (120,30), (255,255,255)) #创建画布 draw = ImageDraw.Draw(img) #设置字体 font = ImageFont.truetype('Arial.ttf', 25) code = getRandomChar() #将生成的字符画在画布上 for t in range(4): draw.text((30*t+5,0),code[t],getRandomColor(),font) #生成干扰点 for _ in range(random.randint(0,50)): #位置,颜色 draw.point((random.randint(0, 120), random.randint(0, 30)),fill=getRandomColor()) #使用模糊滤镜使图片模糊 img = img.filter(ImageFilter.BLUR) #保存 #img.save(''.join(code)+'.jpg','jpeg') return img,codeif __name__ == '__main__': create_code()
设置存放图片的地址
views.py(部分)
from SchoolBuy.utils import check_codefrom io import BytesIOfrom django.http import HttpResponse,Http404def create_code_img(request): #在内存中开辟空间用以生成临时的图片 f = BytesIO() img,code = check_code.create_code() request.session['check_code'] = code img.save(f,'PNG') return HttpResponse(f.getvalue())
utls.py(部分)
url(r'^create_code/$',views.create_code_img),
- 解析
在内存中开辟空间存放图片,并将session中的值更新,将图片发送个一个链接,这里用到”create_code/”
显示图片及判断
test_code.html
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>测试验证码</title></head><body><form method="post" action="#"> {% csrf_token %} <img id="check_code_img" src="/create_code/" onclick="refresh_check_code(this)"> <input type="text" name="code"> <input type="submit"></form><script> function refresh_check_code(ths) { ths.src += '?'; //src后面加问好会自动刷新验证码img的src}</script></body></html>
<script> 是用来刷新图片的表单用post方法发送{% csrf_token %} django用来防止跨站脚本攻击的解决办法<img> 标签内的地址为刚才设置的地址
views.py(部分)
from django.shortcuts import render_to_response,renderdef test_code(request): #GET方法返回表单 if request.method == 'GET': return render(request,'test_code.html') #POST方法用来验证提交的验证码是否正确 else: code = request.POST.get('code','') if code == request.session.get('check_code','error'): return HttpResponse("yes") return HttpResponse("no")
urls.py(部分)
url(r'^$',views.test_code),
至此,验证码检验已完成
可能遇到的问题
- session无法使用
默认情况建django是自动开启session的,使用数据库实现,如果提示没有django_session数据表,则通过下面方法解决
python manage.py makemigrationspython manage.py migratepython manage.py createsuperuser
阅读全文
1 0
- Django实现验证码
- 在Django中实现验证码
- django 中验证码登录验证的实现
- Django实现登录验证功能
- Django—验证码
- django 验证码例子
- django验证码
- Django验证码
- Django 验证码
- Django :验证码
- Django中示例验证码的实现总结
- django 验证码(django-simple-captcha)
- django-simple-captcha 验证码插件 和自己实现验证码的
- django下简单表单验证实现上
- Python+django实现邮箱验证登录
- Django 生成登陆验证码
- Django 点击刷新验证码
- 利用Pillow,几行代码实现的最简单的Django页面验证码功能
- C# 与 dll对接
- IAR之快捷键
- BeginPaint和GetDC
- Eclipse背景颜色修改
- YII2 beforeSave not work?
- Django实现验证码
- RxJava 和 RxAndroid 三(生命周期控制和内存优化)
- 继上一篇博客之后,对数独解析方式进行了优化更新。
- 125. Valid Palindrome(C语言版本)
- 剑指Offer_面试题13_在O(1)时间删除链表结点
- java基本语法
- Josephus环
- 调用约定
- 关于HIVE优化的四种方法总结