python - flask -wtf : Using a kwarg to set the initial value for a field

标签 python flask wtforms flask-wtforms

我想使用 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/

相关文章:

python - 切割结构化 numpy 数组

事件 OOPS 实现后的 Python Tkinter

python-3.x - 有没有办法在flask-socketio事件中修改flask session ?

python - 如何使用 WTForms 渲染我的 TextArea?

python - 尽管已导入其包,但访问 Python 模块失败

python - 安装 Google App Engine 后无法运行默认的 python 应用程序

python - 在 Flask 中为文件创建下载链接的最佳方式?

python - 解析 flask-restful 中的整数列表

python - 有没有办法在 Flask WTForms 中创建数据列表字段?

python - 使用 Flask、WTForm、SQLAlchemy 和 Jinja2 的完整多对一示例