Setting default value after initialization in SelectField flask-WTForms
Asked Answered
K

3

14

I would like to pre-select the value for SelectField when displaying it to user. default argument works when it is passed at the time of instantiation but does not work once the field is initialized.

class AddressForm(Form):
    country = SelectField('Country',choices=[('GB', 'Great Britan'), ('US', 'United States')], default='GB')    # works

When I try to use default value to preselect option before presenting the form to user for editing, it doesn't work.

address_form = AddressForm()
address_form.country.default='US'    # doesnot work

Need a solution to set default value to pre-set values before presenting to the user.

Scenario 2: Also does not work

class AddressForm(Form):
        country = SelectField('Country')    # works

address_form = AddressForm()
address_form.country.choices=[('GB', 'Great Britan'), ('US', 'United States')]
address_form.country.default='US'    # doesnot work
Kurt answered 22/3, 2016 at 14:37 Comment(0)
P
15

Once an instance of the form is created, the data is bound. Changing the default after that doesn't do anything. The reason changing choices works is because it affects validation, which doesn't run until validate is called.

Pass default data to the form constructor, and it will be used if no form data was passed. The default will be rendered the first time, then posted the second time if the user doesn't change the value.

form = AddressForm(request.form, country='US')

(If you're using Flask-WTF's Form you can leave out the request.form part.)

Pastrami answered 22/3, 2016 at 14:58 Comment(0)
R
10

I know that you probably fixed the problem. But I think it doesn't work anymore. And because this is the first thing that shows up when googleing the problem, I want to give people with this problem a working solution(at least for me).

To confirm the change of the default choice, we have to add address_form.process() . Thats it!

The full solution would be:

class AddressForm(Form):
        country = SelectField('Country')    # works

address_form = AddressForm()
address_form.country.choices=[('GB', 'Great Britan'), ('US', 'United States')]
address_form.country.default='US'
address_form.process()    # works
Redfield answered 1/1, 2021 at 20:8 Comment(2)
This is awesome, thanks for sharing! One thing to note is that if you are pre-populating the form when rendering by manually setting address_form.state.data = 'CA' for example, the process() method will wipe your manual settings out. You have to update your dropdown default and call process before setting any other input values manually.Longhand
This works for me but removes the CSRF protection as described here: https://mcmap.net/q/828839/-how-to-fix-quot-the-csrf-token-is-missing-quot-in-flask-wtforms. Is there a workaround?Headmistress
P
1
class AddressForm(Form):
    country = SelectField('Country')    # works

address_form = AddressForm()
address_form.country.choices=[('GB', 'Great Britan'), ('US', 'United States')]
address_form.country.default='US'
address_form.process()

this method show the default as well but when i submit the form doesn't submitted , the validated method works correctly

Ploughman answered 1/4, 2021 at 21:35 Comment(1)
For some reason when I try this i get an error saying "process() missing 1 required positional argument: 'formdata'". Is this because my form is a FlaskForm instead of just a Form?Granjon

© 2022 - 2024 — McMap. All rights reserved.