Jason Rahm
Jason Rahm

Reputation: 710

Wtforms method of posting unchecked boolean?

This answer addresses what I'm trying to accomplish: Post the checkboxes that are unchecked

Given the following form fields:

auto = BooleanField('Is autonomous working?', default=False)
beacon = BooleanField('Can it push the beacon?', default=False)

I get a 400 bad request as I am trying to get data from the form in my view that isn't there because the POST doesn't send the boolean field data when the box is unchecked (False)

Looking at the appropriate way to handle this with wtforms and am having trouble finding a solution. Thanks for any insight.

Upvotes: 1

Views: 1331

Answers (1)

Jason Rahm
Jason Rahm

Reputation: 710

I found the answer after modifying my search parameters. Already answered here: BooleanField usage in WTForms - 400 Bad Request

The actual answer code that helped me is this:

if 'base_select' in request.form:
        create_base = 'y'
    else:
        create_base = 'n'

This checks the form to make sure the field was submitted before trying to extract value from it. Hope this helps someone in the future.

Upvotes: 2

Related Questions