我使用Flask
用表单编写了一个flask_wtf
应用程序。我的表单包含字段和子表单。我的目标是使用循环渲染所有字段,并自行处理子表单。是否可以区分字段和子表单(在jinja2模板中使用if语句)?
form.py
from flask_wtf import form, FlaskForm
from wtforms import StringField, FieldList, FormField
class MySubform(Form):
field1 = StringField(label="field1")
class MyForm(Flaskform):
name = StringField(label="Name")
subform = FieldList(FormField(MySubform), min_entries=1)
index.html
{% extends "bootstrap/base.html" %}
{% block content %}
<div class="row">
{% for field in form %}
<div class="form-group">
{{ field.label(class_='col-sm-3 control-label') }}
<div class="col-sm-9">
{{ field(class_='form-control') }}
</div>
</div>
{% endfor %}
</div>
{% endblock %}
答案 0 :(得分:0)
index.html
{% for field in form %}
{% if field.type == 'FieldList' %}
// Process Subfields
{% else %}
// Process everything except FieldList's
{% endif %}
{% endfor %}