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

后端 未结 7 616
-上瘾入骨i
-上瘾入骨i 2020-11-29 04:09

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_fi         


        
7条回答
  •  一整个雨季
    2020-11-29 04:48

    This is a choices settings with SelectField when you use an int, it works like this:

    test_select = SelectField("Test", coerce=int, choices=[(0, "test0"), (1, "test1")], default=1)
    

    or:

    class TestForm(Form):
        test_select = SelectField("Test", coerce=int)
    
    @app.route("/test")
    def view_function():
        form = TestForm()
        form.test_select.choices = [(0, "test0"), (1, "test1")]
        form.test_select.default = 1
        form.process()
    

提交回复
热议问题