WTForms 我可以在初始化字段时添加占位符属性吗? [英] WTForms Can I add a placeholder attribute when I init a field?
问题描述
我想向 WTForms 中的字段添加占位符属性.我该怎么做?
I want to add a placeholder attribute on to the field in WTForms. How can I do it?
abc = TextField('abc', validators=[Required(), Length(min=3, max=30)], placeholder="test")
以上代码无效
如何添加带有值的占位符属性?
How can I add a placeholder attribute with value?
推荐答案
已针对 WTForms 2.1 更新
从 WTForms 2.1(2015 年 12 月)开始,您现在可以通过将 render_kw=
参数用于字段构造函数来设置渲染关键字.
You can now as of WTForms 2.1 (December 2015) set rendering keywords by using the render_kw=
parameter to the field constructor.
所以该字段看起来像:
abc = StringField('abc', [InputRequired()], render_kw={"placeholder": "test"})
注意虽然这是可能的;它确实开始在代码和演示之间架起桥梁;所以要明智地使用它!
Note while this is possible; it does start to bridge the line between code and presentation; so use it wisely!
(旧答案,对于早于 WTForms 2.1 的版本仍然适用)
placeholder
在 WTforms 2.0.x 及更低版本的 Python 构造函数中不受支持.
placeholder
is not supported in the Python constructor in WTforms 2.0.x and below.
但是,您可以在模板中轻松完成此操作:
However, you can do this easily in your template:
{{ form.abc(placeholder="test") }}
这篇关于WTForms 我可以在初始化字段时添加占位符属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!