如何在WTForms RadioField中动态设置默认值? [英] How to dynamically set default value in WTForms RadioField?
问题描述
我正在用Python Flask 框架构建一个网站,我在其中使用 WTForms 。在一种形式中,我有一个 RadioField a>定义如下: 这没有定义任何选择,因为我这样做lateron(它工作得很好): 我现在要为RadioField 设置默认值,之后我设置了它的选择。所以我试着从定义中删除默认值(我不知道是否 不幸的是,这根本没有任何效果。如果我在字段定义中手动设置它,就像我以前做的那样,但是如果我在创建选项之后动态地设置它,那么不是这样。 有人知道我可以在WTForms中动态设置RadioField的默认值?所有的提示都是值得欢迎的! 您需要运行 这是因为 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: This does not have any choices defined, because I do that lateron (which works perfectly fine): I now want to set the default value for the RadioField after I set the choices for it. So I tried removing the default value from the definition (I'm not sure if Unfortunately this has no effect at all. If I set it manually in the Field definition like I do before it works fine, but not if I set it dynamically after I created the choices. Does anybody know how I can dynamically set the default value for a RadioField in WTForms? All tips are welcome! You need to run This is because the 这篇关于如何在WTForms RadioField中动态设置默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ p $ display = RadioField('display',default ='ONE')
code>
myForm = MyForm()
myForm.display.choices = [('ONE','one'),('TWO','two ')]#This work fine
'ONE'
总是可用的选择),并创建了默认值像上面这样:
myForm.display.default ='ONE'
myForm.process()$ c $添加选项并设置
默认
属性:
myForm = MyForm()
myForm.display.choices = [('ONE','one'),('TWO','two')]
myForm.display.default ='ONE'
myForm.process()#进程选择&默认
默认
被传播( RadioField
,已检查
属性)的字段值( process
方法,它在构造函数中调用。display = RadioField('display', default='ONE')
myForm = MyForm()
myForm.display.choices = [('ONE', 'one'), ('TWO', 'two')] # This works fine
'ONE'
is always an available choice) and I create the default value after I create the choices like I do above:myForm.display.default = 'ONE'
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
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.