Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Checkbox WTForms Example (in 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)
@ondoheer

This comment has been minimized.

Show comment
Hide comment
@ondoheer

ondoheer Jul 6, 2014

Thanks, this was precisely what I was looking for

ondoheer commented Jul 6, 2014

Thanks, this was precisely what I was looking for

@AlexFrazer

This comment has been minimized.

Show comment
Hide comment
@AlexFrazer

AlexFrazer Aug 15, 2014

awesome, this helped

awesome, this helped

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost 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 flask.ext.wtf instead i did it from wtforms directly to get it to work

from wtforms import widgets

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 flask.ext.wtf instead i did it from wtforms directly to get it to work

from wtforms import widgets

@edouardberthe

This comment has been minimized.

Show comment
Hide comment
@edouardberthe

edouardberthe Jun 9, 2015

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

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

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

However "from flask.ext.wtf 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

This comment has been minimized.

Show comment
Hide comment
@clime

clime Feb 26, 2016

Thank you, very useful.

clime commented Feb 26, 2016

Thank you, very useful.

@victor-torres

This comment has been minimized.

Show comment
Hide comment

Nice.

@robinclark007

This comment has been minimized.

Show comment
Hide comment
@robinclark007

robinclark007 Apr 13, 2016

thanks , your helped me

thanks , your helped me

@pavopax

This comment has been minimized.

Show comment
Hide comment
@pavopax

pavopax May 26, 2016

can anyone post link to what this looks like?

pavopax commented May 26, 2016

can anyone post link to what this looks like?

@jonathan-kosgei

This comment has been minimized.

Show comment
Hide comment
@jonathan-kosgei

jonathan-kosgei Sep 7, 2016

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

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

@chewarer

This comment has been minimized.

Show comment
Hide comment
@chewarer

chewarer Oct 14, 2016

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 %}
    {{ form.resident.data }}
{% endif %}

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 %}
    {{ form.resident.data }}
{% endif %}
@YohanObadia

This comment has been minimized.

Show comment
Hide comment
@YohanObadia

YohanObadia Jul 30, 2017

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

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

@gianlucascoccia

This comment has been minimized.

Show comment
Hide comment
@gianlucascoccia

gianlucascoccia Nov 9, 2017

Very helpful, thank you!

Very helpful, thank you!

@pickboy900

This comment has been minimized.

Show comment
Hide comment
@pickboy900

pickboy900 Jan 25, 2018

You don't know how valuable it is for me.

Thanks

You don't know how valuable it is for me.

Thanks

@insomniac807

This comment has been minimized.

Show comment
Hide comment
@insomniac807

insomniac807 Mar 22, 2018

Yeah I would like to see how it is rendered in the html template. For me the border only surrounds the first option which makes it look very messy. a shame because the rest works beautifully

Yeah I would like to see how it is rendered in the html template. For me the border only surrounds the first option which makes it look very messy. a shame because the rest works beautifully

@Skillz-Interface

This comment has been minimized.

Show comment
Hide comment

Thank you

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