我使用
Flask-WFT documentation page上提供的render_field宏来跨不同模板呈现长格式的字段.
字段定义如下
year_built = IntegerField(label='Year Built',description='Year built,not the year of a major renovation.',validators=[validators.NumberRange(
min=1700,max=2012,message="Between %(min)s and %(max)s")])
宏看起来像这样……
{% macro render_field(field) %}
{{ field.label }}{{ field(**kwargs)|safe }}{% if field.errors %}
{% for error in field.errors %}
{{ error }}{% endfor %}
{% endif %}
{% endmacro %}
在表单本身中,各个字段使用…
{{ render_field(form.year_built,class="input text")}}
我无法弄清楚的是在宏字段中创建HTML字段字段(** kwargs)的字段对象中使用描述的方法.我知道我可以将关键字传递给render_field函数,但我正在处理包含100多个字段的表单,并在forms.py中设置描述,然后再次将其设置为template.html中的title关键字,这似乎是不必要的补充.我真的想使用宏来显示描述作为标题,如果它在那里或只显示没有标题的字段,如果不是.
有没有办法在field()函数运行之前向kwargs添加新条目?