How to dynamically set default value in WTForms RadioField?

前端 未结 1 1054
栀梦
栀梦 2020-12-31 12:39

I\'m building a website with the Python Flask framework in which I use WTForms. In one form I\'ve got a RadioField defined as follows:

display = RadioField(\         


        
1条回答
  •  囚心锁ツ
    2020-12-31 13:09

    You need to run myForm.process() after adding the choices and setting the default property:

    myForm = MyForm()
    myForm.display.choices = [('ONE', 'one'), ('TWO', 'two')]
    myForm.display.default = 'ONE'
    myForm.process() # process choices & default
    

    This is because the default is propagated to the field value (and, in the case of RadioField, the checked property) in the process method, which is called in the constructor.

    0 讨论(0)
提交回复
热议问题