Created
August 16, 2019 07:49
-
-
Save ariesduanmu/058d7bd8fdb868c4fc21fc65decac3cd to your computer and use it in GitHub Desktop.
Flask MultiSelect CheckBox
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {% extends "bootstrap/base.html" %} | |
| {% import "bootstrap/wtf.html" as wtf %} | |
| {% block title %}test{% endblock %} | |
| {% block content %} | |
| <div class="col-md-4"> | |
| <form action="" method="post" novalidata> | |
| {{ form.hidden_tag() }} | |
| <p> | |
| {{ form.example }}<br> | |
| </p> | |
| <input type="submit" value="提交"> | |
| </form> | |
| <br> | |
| </div> | |
| {% endblock %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from flask import Flask, render_template | |
| from flask_bootstrap import Bootstrap | |
| from flask_wtf import FlaskForm | |
| from wtforms import widgets, RadioField, SelectMultipleField, SubmitField, SelectField | |
| SECRET_KEY = 'development' | |
| bootstrap = Bootstrap() | |
| app = Flask(__name__) | |
| app.config.from_object(__name__) | |
| bootstrap.init_app(app) | |
| class MultiCheckboxField(SelectMultipleField): | |
| widget = widgets.ListWidget(prefix_label=False) | |
| option_widget = widgets.CheckboxInput() | |
| class SimpleForm(FlaskForm): | |
| example = MultiCheckboxField('Label') | |
| # example = RadioField('Label') | |
| @app.route('/',methods=['post','get']) | |
| def hello_world(): | |
| form = SimpleForm() | |
| form.example.choices = [('one', 'one'), ('two', 'two'), ('three', 'three')] | |
| if form.validate_on_submit(): | |
| print(form.example.data) | |
| else: | |
| print(f"Error:{form.errors}") | |
| return render_template('example.html',form=form) | |
| if __name__ == '__main__': | |
| app.run(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment