将任意数据从 FlaskForm 对象传递到模板
Pass arbitrary data from a FlaskForm object to the template
在构建基于 Flask 的小型 Web 应用程序时,我发现需要将任意数据从 Form Field 对象传递到呈现它的模板。但是,我似乎找不到办法做到这一点。
我认为我唯一可以添加此类数据的地方是 kwargs
属性 of WTForms
Field
objects,但我似乎没有办法从模板访问这些属性。
如果您想知道我要完成什么,我正在编写一个模板宏来简化表单呈现,并且我需要从 Form
字段对象传递一些额外数据 - 主要是布局相关,但不会是字段本身的 HTML 属性(这是 kwargs
设计的目的)。
我找到了答案 here 但它本身并不是答案,但我发表了评论。
引用 Crast:
The description
keyword argument of WTForms fields is allowed to be set at field construction, and is not inspected, just copied directly onto the field, and thus can be any value, not just a string, even a custom attribute. If you want to carry over your own metadata, you can simply use this to carry over any data you may want: TextField(..., description={'placeholder': foo', 'class': bar}
(or even a custom class) then use this attribute in your template for any special metadata you want.
是的,我知道将内容和表示分开,description
属性 的目的并不是真正用于这种用途,但这是我发现传递数据的唯一方法回到我使用宏呈现表单的模板。
为了从模板中访问 description
中传递的数据,我做了如下操作:
{% macro render_create_form(form, form_title, enctype=None) %}
<h2>{{ form_title }}</h2>
<form action="" method="post"{% if enctype %} enctype="{{ enctype }}"{% endif %}>
{{ form.hidden_tag() }}
{% for field in form if not field.name == 'csrf_token' %}
{% set class_name = field.description.class %}
{% if field.type == "StringField" or field.type == "PasswordField" or field.type == "BooleanField" or field.type == "SelectField" %}
<div class="{{ class_name }}">{{ field.label }} {{ field }}</div>
{% elif field.type == "NumberField" %}
<div class="{{ class_name }}">{{ field.label }} {{ field(type='number', min=field.description.min, max=field.description.max, placeholder=field.description.placeholder) }}</div>
{% elif field.type == "HiddenField" %}
{{ field }}
{% elif field.type == "SubmitField" %}
<div class="{{ class_name }}">{{ field }}</div>
{% endif %}
{% endfor %}
</form>
{% endmacro %}
在构建基于 Flask 的小型 Web 应用程序时,我发现需要将任意数据从 Form Field 对象传递到呈现它的模板。但是,我似乎找不到办法做到这一点。
我认为我唯一可以添加此类数据的地方是 kwargs
属性 of WTForms
Field
objects,但我似乎没有办法从模板访问这些属性。
如果您想知道我要完成什么,我正在编写一个模板宏来简化表单呈现,并且我需要从 Form
字段对象传递一些额外数据 - 主要是布局相关,但不会是字段本身的 HTML 属性(这是 kwargs
设计的目的)。
我找到了答案 here 但它本身并不是答案,但我发表了评论。
引用 Crast:
The
description
keyword argument of WTForms fields is allowed to be set at field construction, and is not inspected, just copied directly onto the field, and thus can be any value, not just a string, even a custom attribute. If you want to carry over your own metadata, you can simply use this to carry over any data you may want:TextField(..., description={'placeholder': foo', 'class': bar}
(or even a custom class) then use this attribute in your template for any special metadata you want.
是的,我知道将内容和表示分开,description
属性 的目的并不是真正用于这种用途,但这是我发现传递数据的唯一方法回到我使用宏呈现表单的模板。
为了从模板中访问 description
中传递的数据,我做了如下操作:
{% macro render_create_form(form, form_title, enctype=None) %}
<h2>{{ form_title }}</h2>
<form action="" method="post"{% if enctype %} enctype="{{ enctype }}"{% endif %}>
{{ form.hidden_tag() }}
{% for field in form if not field.name == 'csrf_token' %}
{% set class_name = field.description.class %}
{% if field.type == "StringField" or field.type == "PasswordField" or field.type == "BooleanField" or field.type == "SelectField" %}
<div class="{{ class_name }}">{{ field.label }} {{ field }}</div>
{% elif field.type == "NumberField" %}
<div class="{{ class_name }}">{{ field.label }} {{ field(type='number', min=field.description.min, max=field.description.max, placeholder=field.description.placeholder) }}</div>
{% elif field.type == "HiddenField" %}
{{ field }}
{% elif field.type == "SubmitField" %}
<div class="{{ class_name }}">{{ field }}</div>
{% endif %}
{% endfor %}
</form>
{% endmacro %}