zoukankan      html  css  js  c++  java
  • Flask_WTF实现表单

    Flask_WTF实现表单可分为六个步骤:

    ①导入FlaskForm扩展包(from flask_wtf import FlaskForm)

    ②导入StringField,PasswordField,SubmitField (from wtforms import StringField,PasswordField,SubmitField)

    ③定义表单类

    
    
    class RegisterForm(FlaskForm):
        username = StringField('账号:')
        password = PasswordField('密码:')
        password2 = PasswordField('确认密码:')
        submit=SubmitField('提交')
    
    

    ④定义路由

    @app.route('/regist',methods=['GET','POST'])
    def regist_form():
        redist_form=RegisterForm()
        return render_template('registform.html',form=redist_form)

    ⑤html文件中实现表单

    <form method="post">
            {{ form.username.label }}{{ form.username }}<br>
            {{ form.password.label }}{{ form.password }}<br>
            {{ form.password2.label }}{{ form.password2 }}<br>
            {{ form.submit }}
        </form>

    完整代码:

    app.py

    from flask import Flask,render_template,request,flash
    from flask_wtf import FlaskForm
    from wtforms import StringField, PasswordField,SubmitField
    
    app = Flask(__name__)
    app.secret_key='yingjie'
    
    # 定义表单类
    class RegisterForm(FlaskForm):
        username = StringField('账号:')
        password = PasswordField('密码:')
        password2 = PasswordField('确认密码:')
        submit=SubmitField('提交')
    
    @app.route('/regist',methods=['GET','POST'])
    def regist_form():
        redist_form=RegisterForm()
        return render_template('registform.html',form=redist_form)
    
    if __name__ == '__main__':
        app.run()

    registform.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <form method="post">
            {{ form.username.label }}{{ form.username }}<br>
            {{ form.password.label }}{{ form.password }}<br>
            {{ form.password2.label }}{{ form.password2 }}<br>
            {{ form.submit }}
        </form>
    </body>
    </html>

    运行结果:

  • 相关阅读:
    在网页中象GMAIL一样检测客户端是否连接到网络
    How to adjust IFrame height on it's content (转载)
    asp.net工程中aspx文件与codebehind文件的关联问题
    一个在VC中调用web service的方法
    往mssql 表的自增长字段中添加值
    iis7上安装php5+mysql
    试用淘宝开放平台
    关于IOCP的方方面面
    Overlapped I/O
    为什么需要allocator的rebind接口
  • 原文地址:https://www.cnblogs.com/zyj3955/p/15368164.html
Copyright © 2011-2022 走看看