我研究了与python3 flask和jinja结合使用的wtforms的bind()函数/方法。 我的目标是在表单上实时渲染一个隐藏字段,该字段包含在基本模型中。我已经花了几天时间寻找解决方案,但是我不清楚。绑定未绑定字段如何工作?
我尝试过的一小段内容
# self.model.append_field(
name="multiselector_results",
field=HiddenField()
)
tmp = HiddenField(
label="multiselector_results",
validators=[],
description="",
id="multiselector_results",
default=""
)
tmp.bind(
self.model,
"multiselector_results"
)
# self.model.multiselector_results.bind(self, tmp)
# self.model._fields["multiselector_results"] = self.model.multiselector_results.bind(
self, HiddenField(), {"name": "multiselector_results"})
# self.model.multiselector_results.bind()
我的主要Form类包括此功能
@classmethod
def append_field(cls, name, field):
setattr(cls, name, field)
return cls
append_field
函数位于名为Form
的主类中,每个模型基本上都从该主类中继承。
我能够即时将隐藏字段分配给我的模型,以验证append_field
方法是否确实有效,但是由于某些原因,隐藏字段本身仍保持未绑定状态。
对于我来说,文档还不够清楚,因为即使ctrl
+ F
也带来了bind()
的一击。
以下是我研究过的一些参考资料:
http://wtforms.simplecodes.com/docs/0.6/fields.html
https://wtforms.readthedocs.io/en/latest/forms.html#wtforms.form.Form.__init
https://media.readthedocs.org/pdf/wtforms/2.0.2/wtforms.pdf
https://media.readthedocs.org/pdf/wtforms/1.0.5/wtforms.pdf
http://wtforms.simplecodes.com/docs/1.0.2/fields.html
https://groups.google.com/forum/#!topic/wtforms/cJl3aqzZieA
我还发现了几个堆栈溢出问题,虽然这个问题有些不同,但还是很可怕的。这些解决方案给了我新的尝试尝试的灵感。示例主题:
How to bind a field in __init__ function of a form
我确实喜欢davidism
提供的其他解决方案,但就我而言,这是不可能的。
只有整个wtforms文档中我发现的有关bind()
If _form and _name isn’t provided, an UnboundField will be returned instead. Call its
bind() method with a form instance and a name to construct the field.
如何构造未绑定的字段?