• Flask表单验证


    学习内容:①判断请求方式(request.method)

    from flask import Flask,render_template,request
    
    app = Flask(__name__)
    
    
    @app.route('/',methods=['GET','POST'])
    def hello_world():
        # 判断请求方式
        if request.method=='POST':
        return render_template('index.html')
    
    
    if __name__ == '__main__':
        app.run()

         

         ②获取表单信息(request.form.get())

    from flask import Flask,render_template,request
    
    app = Flask(__name__)
    
    
    @app.route('/',methods=['GET','POST'])
    def hello_world():
        # 判断请求方式
        if request.method=='POST':
            #获取请求参数
            username=request.form.get('username')
            password=request.form.get('password')
            password2=request.form.get('password2')
            print('username:',username)
            print('password:',password)
            print('password2:',password2)
            return 'success'
        return render_template('index.html')
    
    
    if __name__ == '__main__':
        app.run()

         

         ③判断表但是否填写完整(if not all())

    from flask import Flask,render_template,request
    
    app = Flask(__name__)
    
    
    @app.route('/',methods=['GET','POST'])
    def hello_world():
        # 判断请求方式
        if request.method=='POST':
            #获取请求参数
            username=request.form.get('username')
            password=request.form.get('password')
            password2=request.form.get('password2')
            print('username:',username)
            print('password:',password)
            print('password2:',password2)
            # 判断表但是否填完整
            if not all([username,password,password2]):
                print("表单未填完整")
            return 'success'
        return render_template('index.html')
    
    
    if __name__ == '__main__':
        app.run()

         

         ④判断两次输入密码是否一致

    from flask import Flask,render_template,request
    
    app = Flask(__name__)
    
    
    @app.route('/',methods=['GET','POST'])
    def hello_world():
        # 判断请求方式
        if request.method=='POST':
            #获取请求参数
            username=request.form.get('username')
            password=request.form.get('password')
            password2=request.form.get('password2')
            print('username:',username)
            print('password:',password)
            print('password2:',password2)
            # 判断表但是否填完整
            if not all([username,password,password2]):
                print("表单未填完整")
            # 判断两次密码是否一致
            if password!=password2:
                print("两次密码不一致,请重新输入!")
            return 'success'
        return render_template('index.html')
    
    
    if __name__ == '__main__':
        app.run()

     

    完整代码:

    app.py:

    from flask import Flask,render_template,request
    
    app = Flask(__name__)
    
    
    @app.route('/',methods=['GET','POST'])
    def hello_world():
        # 判断请求方式
        if request.method=='POST':
            #获取请求参数
            username=request.form.get('username')
            password=request.form.get('password')
            password2=request.form.get('password2')
            print('username:',username)
            print('password:',password)
            print('password2:',password2)
            # 判断表但是否填完整
            if not all([username,password,password2]):
                print("表单未填完整")
            # 判断两次密码是否一致
            if password!=password2:
                print("两次密码不一致,请重新输入!")
            return 'success'
        return render_template('index.html')
    
    
    if __name__ == '__main__':
        app.run()

    index.html:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
            <form method="post" action="/">
               <label> 账号:</label><input type="text" name="username"><br>
               <label> 密码:</label><input type="password" name="password"><br>
                <label> 确认密码:</label><input type="password" name="password2"><br>
                <input type="submit" name="submit" value="登录">
                <input type="reset" name="reset" value="重置">
            </form>
    </body>
    </html>

    运行截图:

     

     

     

  • 相关阅读:
    用 HTML 格式导出 Excel 时,如何保留显示网格线
    [转载] 让SciTE能够和IDE一样进行成员提示
    RedHat 上安装多个 mysql 实例并配置 django 连接的操作记录
    我的 SciTEGlobal.properties 配置文件
    FrameSet 不能支持透明
    RedHat 上安装 lighttpd 并配置 fastcgi + django 的记录
    系统设计与开发 辅助工具大集成终结版
    SQL Server Management Object(SMO)大大简化数据库工具的开发 几行代码开发功能强大的SQL工具
    Enterprise Solution 解决方案与源代码下载
    LLBL Gen 实体映射工具技术原理剖析
  • 原文地址:https://www.cnblogs.com/zyj3955/p/15367658.html
Copyright © 2020-2023  润新知