Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Checkbox WTForms Example (in Flask)
from flask import Flask, render_template
from import Form, widgets, SelectMultipleField
SECRET_KEY = 'development'
app = Flask(__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)
def hello_world():
form = SimpleForm()
if form.validate_on_submit():
print form.errors
return render_template('example.html',form=form)
if __name__ == '__main__':

ondoheer commented Jul 6, 2014

Thanks, this was precisely what I was looking for

awesome, this helped


ghost commented Nov 5, 2014

Thanks for the snippet - was super helpful. Not sure if they modified the flask-wtf extension, but I couldnt import widgets from instead i did it from wtforms directly to get it to work

from wtforms import widgets

Thank you, it's exactly what I'm looking for ! :)

However "from import widgets" didn't work for me, because there was neither "ListWidget" nor "CheckboxInput" in it.
I had to do "from wtforms import widgets"

clime commented Feb 26, 2016

Thank you, very useful.


thanks , your helped me

pavopax commented May 26, 2016

can anyone post link to what this looks like?

How to set the first field in the select box to be disabled and something like 'Choose your option' ?

Thanks for the example.
It's my code:

choices = [(1, 'one'),
           (2, 'two'),
           (3, 'tree')]
resident = MultiCheckboxField('Label',
                              choices=choices, coerce=int)

In my form this checkboxes displayed as intended. But on page with form results, I can render only key of choice(number). How to render a text value of choice?
In Jinja template:

{% if form.resident %}
    {{ }}
{% endif %}

Would have been great to see how your example.html is made.

Very helpful, thank you!

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