Pre-populating a BooleanField as checked (WTForms)
--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Ocean Floor
--
Chapters
00:00 Pre-Populating A Booleanfield As Checked (Wtforms)
01:17 Accepted Answer Score 13
01:34 Answer 2 Score 8
01:55 Answer 3 Score 2
02:36 Answer 4 Score 12
02:45 Thank you
--
Full question
https://stackoverflow.com/questions/1975...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #flask #wtforms
#avk47
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Ocean Floor
--
Chapters
00:00 Pre-Populating A Booleanfield As Checked (Wtforms)
01:17 Accepted Answer Score 13
01:34 Answer 2 Score 8
01:55 Answer 3 Score 2
02:36 Answer 4 Score 12
02:45 Thank you
--
Full question
https://stackoverflow.com/questions/1975...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #flask #wtforms
#avk47
ACCEPTED ANSWER
Score 13
If you have an object you can use it to populate your form like form = QuestionForm(obj=my_obj). If you only want to set the active attribute use form = QuestionForm(active=True).
ANSWER 2
Score 12
A BooleanField defined like:
checkbox = BooleanField('title',
default=True,
render_kw ={'checked':''})
ANSWER 3
Score 8
snahor's answer helped after much searching (+1). The google seems weak on this question. I found I needed
<div class="form-group">
{{adminForm.is_admin.label}}
{{adminForm.is_admin(checked=True, class_="form-control")}}
</div>
<div class="form-group">
{{adminForm.is_admin.label}}
{{adminForm.is_admin(checked=False, class_="form-control")}}
</div>
which I have utilised as
<div class="form-group">
{{adminForm.is_admin.label}}
{{adminForm.is_admin(checked=user.is_admin, class_="form-control")}}
</div>
ANSWER 4
Score 2
To have the default boolean value as True, you need to set the default to "checked"
Basic fields Basic fields generally represent scalar data types with single values, and refer to a single input from the form.
class wtforms.fields.BooleanField(default field arguments, false_values=None)Represents an input type="checkbox". Set the checked-status by using the default-option. Any value for default, e.g. default="checked" puts checked into the html-element and sets the data to True
class QuestionForm(Form):
question = TextField('Question', [validators.Required()])
slug = TextField('Slug', [validators.Required()])
active = BooleanField('Active', default="checked")