Skip to content

Instantly share code, notes, and snippets.

@juzten
Forked from doobeh/example.html
Created April 11, 2015 07:48
Show Gist options
  • Save juzten/2c7850462210bfa540e3 to your computer and use it in GitHub Desktop.
Save juzten/2c7850462210bfa540e3 to your computer and use it in GitHub Desktop.
custom multiple checkbox field for wtforms and flask
from flask import Flask, render_template
from flask.ext.wtf import Form, widgets, SelectMultipleField
SECRET_KEY = 'development'
app = Flask(__name__)
app.config.from_object(__name__)
class MultiCheckboxField(SelectMultipleField):
widget = widgets.ListWidget(prefix_label=False)
option_widget = widgets.CheckboxInput()
class SimpleForm(Form):
string_of_files = ['one\r\ntwo\r\nthree\r\n']
list_of_files = string_of_files[0].split()
# create a list of value/description tuples
files = [(x, x) for x in list_of_files]
example = MultiCheckboxField('Label', choices=files)
@app.route('/',methods=['post','get'])
def hello_world():
form = SimpleForm()
if form.validate_on_submit():
print form.example.data
else:
print form.errors
return render_template('example.html',form=form)
if __name__ == '__main__':
app.run(debug=True)
@EduGord
Copy link

EduGord commented Sep 10, 2020

Very helpful,
Just in case somebody is having problems rendering MultiCheckboxField and you don't want to change your CSS just to include that, I'd recommend adding render_kw param to parse inline style properties.

e.g;
MultiCheckboxField('My Label', choices=list_of_tuples, render_kw={'style': 'height: fit-content; list-style: none;'})

@allisonworks
Copy link

Very helpful, Just in case somebody is having problems rendering MultiCheckboxField and you don't want to change your CSS just to include that, I'd recommend adding render_kw param to parse inline style properties.

e.g; MultiCheckboxField('My Label', choices=list_of_tuples, render_kw={'style': 'height: fit-content; list-style: none;'})

Thank you!, This is a big help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment