• 简单的flask-wtf 表单设置及后台获取


    简单的flask-wtf 表单设置及后台获取

    #!/usr/bin/env python
    
    from flask import Flask, render_template_string
    from flask_wtf import FlaskForm
    from wtforms.fields import BooleanField, DecimalField, IntegerField, 
        PasswordField, RadioField, SelectMultipleField, SelectField, StringField, 
        SubmitField, TextAreaField
    
    application = app = Flask('wsgi')
    app.config['SECRET_KEY'] = '1234567890'
    
    
    class LanguageForm(FlaskForm):
        string = StringField(label="文本字段")
        boolean = BooleanField(label='布尔选择')
        decimal = DecimalField(label='小数数字')
        integer = IntegerField(label='整数')
        radio = RadioField(label='单选', choices=[('M', ''), ('F', '')])  # 不能重新赋值。
        select = SelectField(label='下拉单选', choices=[('a', 'A'), ('b', 'B'), ('c', 'C')])  # 下拉单选,重新赋值出错。
        textarea = TextAreaField(label='多行文本')
        password = PasswordField(label='密码')
        submit = SubmitField(label='按钮')
        selectmultiple = SelectMultipleField(label='多选', choices=[('a', 'A'), ('b', 'B'), ('c', 'C')])  # 使用ctrl进行的多选
    
    
    template_form = """
    {% block content %}
    <h1>测试flask-wtf</h1>
    <form method="POST" action="/">
        {{ form.csrf_token }}
        <div>{{ form.string.label }} {{ form.string }}</div>
        <div>{{ form.boolean.label }} {{ form.boolean }}</div>
        <div>{{ form.decimal.label }} {{ form.decimal }}</div>
        <div>{{ form.integer.label }} {{ form.integer }}</div>
        <div>{{ form.radio.label }} {{ form.radio }}</div>
        <div>{{ form.select.label }} {{ form.select }}</div>
        <div>{{ form.textarea.label }} {{ form.textarea }}</div>
        <div>{{ form.password.label }} {{ form.password }}</div>
        <div>{{ form.selectmultiple.label }} {{ form.selectmultiple }}</div>
        <div>{{ form.submit.label }} {{ form.submit }}</div>
        <button type="submit" class="btn">提交</button>    
    </form>
    {% endblock %}
    
    """
    
    
    @app.route('/', methods=['GET', 'POST'])
    def index():
        form = LanguageForm()
        if form.validate_on_submit():
            print(form.string.data)
            print(form.boolean.data)
            print(form.decimal.data)
            print(form.integer.data)
            print(form.radio.data)
            print(form.select.data)
            print(form.textarea.data)
            print(form.password.data)
            print(form.submit.data)
            print(form.selectmultiple.data)
            return render_template_string(template_form, form=form)
        else:
            print('GET')
            return render_template_string(template_form, form=form)
    
    
    if __name__ == '__main__':
        app.run(
            host='0.0.0.0',
            debug=True)
  • 相关阅读:
    php json_encode JSON_UNESCAPED_UNICODE
    ubuntu 添加多个ssh公钥和私钥
    如何自动化新增配置文件呢?
    git配置ssh公钥
    项目PHP新知识点
    mysql 数据库复制表 create table city1 like city;
    .NET 泛型集合数据写CSV文件
    .NET C# 泛型队列
    逆向地理编码--根据地址搜索定位,点击地图、获取经纬度信息
    正向地理编码-根据输入地址获取经纬度
  • 原文地址:https://www.cnblogs.com/jackadam/p/12167430.html
Copyright © 2020-2023  润新知