• 关于Flask高级_WTF自定义验证器的方法


    Flask高级_WTF自定义验证器

    一.自定义验证器步骤

    1. 定义一个方法,方法的名字规则是: validate_字段名(self,field) 。
    2. 在方法中,使用 field.data 可以获取到这个字段的具体的值(前端的)。
    3. 验证时,如果数据满足条件,那么可以什么都不做。如果验证失败,那么应该抛出一个 wtforms.validators.ValidationError 的异常,并且把验证失败 的信息传到这个异常类中。

    二.实例

    DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>登录页面title>
    head>
    <body>
        <form action="/login/" method="post">
            <table>
                <tr>
                    <th>用户名:th>
                    <td><input type="text" name="uname">td>
                tr>
                <tr>
                    <th>密码:th>
                    <td><input type="text" name="pwd">td>
                tr>
                <tr>
                    <th>验证码:th>
                    <td><input type="text" name="code" maxlength="4"> <span style="background-color: aqua;">{{code}}span>td>
                tr>
                <tr>
                    <th>th>
                    <td><input type="submit" value="登录">td>
                tr>
            table>
        form>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    loginForm.py:
    from flask import session
    from wtforms import Form,StringField
    from wtforms.validators import Length,ValidationError
    
    class loginForm(Form):
        code = StringField(validators=[Length(4,4)])
    
        def validate_code(self,field):
            font_code = field.data
            server_code = str(session.get('code'))
            if font_code != server_code:
                raise ValidationError('验证码不一致!!!请重新输入!')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    main.py:
    #coding=utf-8
    
    from flask import Flask,render_template,request,session
    from random import randint
    
    from loginForm import loginForm
    
    app = Flask(__name__)
    
    app.secret_key = 'adada'
    
    @app.route('/')
    def index():
        return 'Hello~'
    
    @app.route('/login/',methods=['GET','POST'])
    def login():
        if request.method == 'GET':
            code = randint(1000,9999)
            session['code'] = code
            return render_template('login.html',code=code)
        else:
            form = loginForm(request.form)
            if form.validate():
                return '验证成功!'
            return f'验证失败!失败信息:{form.errors}'
    if __name__ == '__main__':
        app.run(debug=True)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    在这里插入图片描述
    在这里插入图片描述

    注:
    如果觉得笔记有些问题,麻烦在百忙之中在评论中指正,或提出建议!另外,如果觉得这份笔记对你有所帮助,麻烦动动发财的小手手点一波赞!
  • 相关阅读:
    Credit-based Flow Control的前世今生
    (2022版)一套教程搞定k8s安装到实战 | 临时容器
    20个Python面试题来挑战你的知识
    动态规划合集
    RK3568平台 IIC子系统框架
    云原生 | 服务网格宝藏工具大推荐!
    【萌新向】Sql Server保姆级安装教程-图文详解手把手教你安sql
    神经网络(neural network)
    全面解析各类营养物质在炎症中的作用
    FPGA 学习笔记:Vivado 配置IO引脚约束
  • 原文地址:https://blog.csdn.net/qq_55961861/article/details/126607718