How do you set a default value for a WTForms SelectField?

0

Issue

When attempting to set the default value of a SelectField with WTForms, I pass in value to the ‘default’ parameter like so.

class TestForm(Form):
  test_field = SelectField("Test: ", choices=[(1, "Abc"), (2, "Def")], default=2)

I have also tried the following.

class TestForm(Form):
  test_field = SelectField("Test: ", choices=[(1, "Abc"), (2, "Def")], default=(2, "Def"))

Neither set the default selected field to “Def.” This works for other kinds of Fields such as TextField. How do you set the default value for a SelectField?’

Solution

The first way you posted is correct, and it works for me. The only explanation for it not working can be that you are running an older version of WTForms, it worked for me on 1.0.1

Answered By – sp.

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More