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
原创粉丝点击