简单的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)