zoukankan      html  css  js  c++  java
  • Django

    用自己的表继承user表

    from django.contrib.auth.models import AbstractUser
    class UserInfo(AbstractUser):
        tel = models.CharField(max_length=32)

    并在settings中配置

    AUTH_USER_MODEL = 'app01.UserInfo'

     用PIL生成随机验证码图片

    import random
    from PIL import Image, ImageDraw, ImageFont
    from io import BytesIO
    
    
    # 验证码视图
    def get_valid_img(request):
    
        def get_random_color():
            return (
                random.randint(
                    0, 255), random.randint(
                    0, 255), random.randint(
                    0, 255))
        #
        # img=Image.new("RGB",(350,38),get_random_color())
        # f=open("valid.png","wb")
        # img.save(f,"png")
        # with open("valid.png","rb") as f:
        #     data=f.read()
    
        # 方式3:
        # img=Image.new("RGB",(350,38),get_random_color())
        # f=BytesIO()
        # img.save(f,"png")
        # data=f.getvalue()
    
        # # 方式4:完善文本
        #
        # img=Image.new("RGB",(350,38),get_random_color())
        # draw=ImageDraw.Draw(img)
        # font=ImageFont.truetype("static/font/kumo.ttf",32)
        # draw.text((0,0),"python!",get_random_color(),font=font)
        #
        # # 写与读
        # f=BytesIO()
        # img.save(f,"png")
        # data=f.getvalue()
    
        # 方式5:
    
        img = Image.new("RGB", (125, 38), get_random_color())
        draw = ImageDraw.Draw(img)
        font = ImageFont.truetype("static/font/kumo.ttf", 32)
    
        keep_str = ""
        for i in range(5):
            random_num = str(random.randint(0, 9))
            random_lowalf = chr(random.randint(97, 122))
            random_upperalf = chr(random.randint(65, 90))
            random_char = random.choice(
                [random_num, random_lowalf, random_upperalf])
            draw.text((i * 25, 0), random_char, get_random_color(), font=font)
            keep_str += random_char
    
        width = 125
        height = 38
        for i in range(3):
            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())
        # 写与读
        f = BytesIO()
        img.save(f, "png")
        data = f.getvalue()
    
        print('keep_str', keep_str)
    
        # 将验证码存在各自的session中
    
        request.session['keep_str'] = keep_str
    
        return HttpResponse(data)

    此视图函数对应的url即为图片地址,校验时通过session取到的值与ajax的发送数据进行比较,注意大小写,建议使用upper比较大写

  • 相关阅读:
    【函数】wm_concat包的订制
    【云和恩墨】性能优化:Linux环境下合理配置大内存页(HugePage)
    【技巧】如何使用客户端发布BLOG+如何快速发布微信公众号文章
    【故障处理】队列等待之TX
    【转载】TX
    【转载】Linux磁盘管理:LVM逻辑卷管理
    【索引】Oracle之不可见索引和虚拟索引的比对
    小麦苗微信公众号文章链接地址
    Oracle 11g新特性direct path read引发的系统停运故障诊断处理
    常识之外:全表扫描为何产生大量 db file sequential read 单块读?
  • 原文地址:https://www.cnblogs.com/jiaqi-666/p/9910792.html
Copyright © 2011-2022 走看看