我想使用 kwarg 来设置表单字段的初始值。我以前做过类似的事情:
from wtforms import Form
from wtforms.fields import StringField
class BasicForm(Form):
inputField = StringField("Name")
basic_form_kwargs = {"inputField" : "Example"}
basic_form = BasicForm(**basic_form_kwargs)
print basic_form.inputField
#<input id="inputField" name="inputField" type="text" value="Example">
这段代码按我的预期工作。 value 属性设置为“Example”。但是,当我开始使用 FormFields 时,它并没有按我的预期工作。
from wtforms import Form
from wtforms.fields import StringField
class ChildForm(Form):
inputField = StringField("Name")
class ParentForm(Form):
childWrapper = FormField(ChildForm)
complex_form_kwargs = {"childWrapper-inputField" : "Example"}
complex_form = ParentForm(**complex_form_kwargs)
print complex_form.childWrapper.inputField
#<input id="childWrapper-inputField" name="childWrapper-inputField" type="text" value="">
使用 FormFields 时需要传递什么 kwarg 才能设置 inputField 的值?
最佳答案
就像您将字典传递到父级表单中一样,您需要将字典传递到子表单中,如下所示:
kwargs = {"childWrapper": {"inputFIeld": "Example2"}}
form = ParentForm(**kwargs)
关于python - flask -wtf : Using a kwarg to set the initial value for a field,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30854456/