How to get value from a BooleanField in Flask-WTF?

Viewed 1036

I get the value of a BooleanField but it's always False, even if it's checked. I think I missed something.

My view :

@bp.route('/update/<int:id>', methods=['GET', 'POST'])
@login_required
note = Notes.query.get_or_404(id)
form = NoteForm()
if form.validate_on_submit():
    note.privacy = form.privacy.data # Always got False value, even when it's checked
    # ...

Here is the form:

# Some imports
class NoteForm(FlaskForm):
    privacy = BooleanField('Private', default=False, validators=[AnyOf([True, False])])
    # ...

And the template:

<div class="field">
    {{ form.privacy(class="switch") }}
    {{ form.privacy.label }}
</div>
1 Answers

My checkbox was outside of the form for design needs. Just needed to link the field to my wtf form to resolve the problem:

<div class="field">
    {{ form.privacy(class="switch", form='noteform') }}
    {{ form.privacy.label }}
</div>
Related