gpt4 book ai didi

python - flask-wtform 占位符行为

转载 作者:太空宇宙 更新时间:2023-11-03 15:06:35 25 4
gpt4 key购买 nike

表格:

class SignUpForm(Form):
username = TextField("Username: ",validators=[Required(),Length(3,24)])

为什么会这样?

form = SignUpForm()
form.username(placeholder="username")

但当您直接使用占位符作为 SignUpForm 的参数时不是吗?

class SignUpForm(Form):
username = TextField("Username: ",placeholder="username",validators=[Required(),Length(3,24)])

它给出了这个错误 TypeError: __init__() got an unexpected keyword argument 'placeholder'

我对此有点困惑,因为直接在类上定义它应该与执行 form.username(placeholder="username") 但是为什么会报错?

最佳答案

Calling a field to render it accepts arbitrary keyword args to add attributes to the input.如果你想要一个快捷方式来渲染一个以标签作为占位符的字段,你可以编写一个 Jinja 宏。

{% macro form_field(field) %}
{{ field(placeholder=field.label.text) }}
{% endmacro %}

您还可以在通过设置 render_kw 参数定义字段时提供参数以传递给每个渲染调用。

username = TextField(render_kw={"placeholder": "username"})

关于python - flask-wtform 占位符行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31954547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com