Skip to content

Instantly share code, notes, and snippets.

@ariesduanmu
Created August 16, 2019 07:49
Show Gist options
  • Select an option

  • Save ariesduanmu/058d7bd8fdb868c4fc21fc65decac3cd to your computer and use it in GitHub Desktop.

Select an option

Save ariesduanmu/058d7bd8fdb868c4fc21fc65decac3cd to your computer and use it in GitHub Desktop.
Flask MultiSelect CheckBox
{% 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 %}
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