2018.7.8-表单方面的问题以及验证码的生成

解除某些浏览器自动验证表单错误与否的方法

在form标签后面加上novalidate,例如:

<form action="#" mathod="POST" novalidate>    <form/>

表单的后台创建

1.创建LoginForm类继承自django.forms中的Form:

class LoginForm(Form):
    username = fields.CharField(
        widget=widgets.TextInput(attrs={'class': 'form-control', 'placeholder': '填写用户名...'}),
        error_messages={
            'required': '用户名不能为空!',
        }
    )
    password = fields.CharField(
        widget=widgets.PasswordInput(attrs={'class': 'form-control', 'placeholder': '填写密码...'}),
        error_messages={
            'required': '密码不能为空!',
        }
    )

  2.直接将LoginForm实例化成login_form对象传到前台使用:

login_form = LoginForm()
        return render(request, 'login.html', {'login_form': login_form})

  如果需要保存上一次用户输入的文字信息,则在实例化的时候传入POST信息即可:

login_form = LoginForm(request.POST)

验证码的实现

1.验证码工具方法的简单实现(利用pillow模块)

def check_code(width=120, height=30, char_length=5, font_file='kumo.ttf', font_size=28):
    code = []
    img = Image.new(mode='RGB', size=(width, height), color=(255, 255, 255))
    draw = ImageDraw.Draw(img, mode='RGB')
 
    def rndChar():
        """
        生成随机字母   
        :return:
        """
        return chr(random.randint(65, 90))
 
    def rndColor():
        """
        生成随机颜色
        :return:
        """
        return (random.randint(0, 255), random.randint(10, 255), random.randint(64, 255))
 
    # 写文字
    font = ImageFont.truetype(font_file, font_size)
    for i in range(char_length):
        char = rndChar()
        code.append(char)
        h = random.randint(0, 4)
        draw.text([i * width / char_length, h], char, font=font, fill=rndColor())
 
    # 写干扰点
    for i in range(40):
        draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())
 
    # 写干扰圆圈
    for i in range(40):
        draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())
        x = random.randint(0, width)
        y = random.randint(0, height)
        draw.arc((x, y, x + 4, y + 4), 0, 90, fill=rndColor())
 
    # 画干扰线
    for i in range(5):
        x1 = random.randint(0, width)
        y1 = random.randint(0, height)
        x2 = random.randint(0, width)
        y2 = random.randint(0, height)
 
        draw.line((x1, y1, x2, y2), fill=rndColor())
 
    img = img.filter(ImageFilter.EDGE_ENHANCE_MORE)
    return img,''.join(code)

2.利用BytesIO模块将生成的验证码图片暂存至内存,将正确的验证码存入session以用来验证,最后利用HttpResponse将内存中的验证码送至前端:

def get_code(request):
    img, code = random_check_code.check_code()
    stream = BytesIO()
    img.save(stream, 'png')
    request.session['code'] = code
    return HttpResponse(stream.getvalue())

遇到的一个小问题:python错误:TypeError: 'module' object is not callable

由于我的粗心大意,将导入的‘random_check_code’模块直接作为了一个函数来使用,但它却无法调用。事实上,调用的应该是random_check_code模块中check_code这个方法。

补充import导入的两种方式:

(1)导入方式:

写法1:from Corpus import TrainCorpusStructure, 

写法2:import corpusProcess.TrainCorpusStructure as tc

(2)调用方式不同:

写法1: tcs = TrainCorpusStructure.TrainCorpusStructure(),

写法2: tcs = tc.TrainCorpusStructure()

posted on 2018-07-08 16:20  yujie158392613  阅读(118)  评论(0编辑  收藏  举报

导航