• Django实现验证码


    简单搞定生成验证码:

    1.views.py

    from io import BytesIO
    import random
    from PIL import Image,ImageDraw,ImageFont
    from utils.check_code import create_validate_code
    
    def checkCode(request):
        return render(request,'code_test.html')
    
    def codetest(request):
        # # 获取随机颜色的函数
        # def get_random_color():
        #     return random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)
        #
        # # 生成一个图片对象
        # img_obj = Image.new(
        #     'RGB',
        #     (220, 35),
        #     get_random_color()
        # )
        # # 在生成的图片上写字符
        # # 生成一个图片画笔对象
        # draw_obj = ImageDraw.Draw(img_obj)
        # # 加载字体文件, 得到一个字体对象
        # font_obj = ImageFont.truetype('arial.ttf', 28)
        # # 开始生成随机字符串并且写到图片上
        # tmp_list = []
        # for i in range(5):
        #     u = chr(random.randint(65, 90))  # 生成大写字母
        #     l = chr(random.randint(97, 122))  # 生成小写字母
        #     n = str(random.randint(0, 9))  # 生成数字,注意要转换成字符串类型
        #
        #     tmp = random.choice([u, l, n])
        #     tmp_list.append(tmp)
        #     draw_obj.text((20 + 40 * i, 0), tmp, fill=get_random_color(), font=font_obj)
        #
        # # 保存到session
        # request.session["valid_code"] = "".join(tmp_list)
        # # 加干扰线
        # width = 220  # 图片宽度(防止越界)
        # height = 35
        # 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_obj.line((x1, y1, x2, y2), fill=get_random_color())
        #
        # # 加干扰点
        # for i in range(40):
        #     draw_obj.point((random.randint(0, width), random.randint(0, height)), fill=get_random_color())
        #     x = random.randint(0, width)
        #     y = random.randint(0, height)
        #     draw_obj.arc((x, y, x + 4, y + 4), 0, 90, fill=get_random_color())
        #
        # # 不需要在硬盘上保存文件,直接在内存中加载就可以
        # io_obj = BytesIO()
        # # 将生成的图片数据保存在io对象中
        # img_obj.save(io_obj, "png")
        # # 从io对象里面取上一步保存的数据
        # data = io_obj.getvalue()
        # return HttpResponse(data)
    View Code

    2.url

    urlpatterns = [
        path('admin/', admin.site.urls),
        url('^checkcode.html$',views.checkCode),
        url('^codetest.html',views.codetest),
    ]
    View Code

    3.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>验证码测试</title>
    </head>
    <body>
        <p><input type="text" placeholder="用户名"></p>
        <p><input type="text" placeholder="密码"></p>
        <p><input type="text" placeholder="验证码">
            <img src="/static/img/20181207212735.png" alt="">
            <img src="/codetest.html" alt="">
        </p>
    </body>
    </html>
    View Code
  • 相关阅读:
    python3 bs4库
    python3 requests 库学习
    thinkPHP远程代码执行漏洞
    centos 数据库忘记密码
    centos 安装 nfs
    中间人工具---MITMF
    微信公众号授权获取codequ
    vue element 倒计时
    vue常见登陆注册按钮切换效果
    VUE3.0重新定向
  • 原文地址:https://www.cnblogs.com/ray-h/p/10229094.html
Copyright © 2020-2023  润新知