如何在WTForms中渲染可变长度的FieldList?

时间:2018-05-09 14:06:22

标签: python python-2.7 jinja2 flask-wtforms wtforms

我正在尝试制作一个表单来填充日历。日历可以是五天或七天,我想将天数作为参数传递给表单。我的表单定义如下:

class LoadForecastForm(Form):

    def __init__(self, n_days, *args, **kwargs):
        self.n_days = n_days
        super(LoadForecastForm, self).__init__(*args, **kwargs)
        self.day_values = FieldList(SelectField('Day Values', coerce=str, choices=[('low', 'Low'), ('medium', 'Medium'), ('high', 'High'), ('holiday', 'Holiday')]), min_entries=n_days, max_entries=n_days)
        self.send = SubmitField('Send Calendar')

它呈现如下:

<form role="form" action="" method="POST">
{{ form.hidden_tag() }}
<div class="form-group row">
  {% for field in form.day_values %}
  <div class="col-sm-2">
    <div class="form-group{% if field.errors %} error {% endif %}">
      {% if label %}
        {{ field.label }}
      {% endif %}
      {{ field(**kwargs) }}
      {% for error in field.errors %}
        <span class="help-inline">[{{error}}]</span><br>
      {% endfor %}
    </div>
  </div>
  {% endfor %}
</div>
{{ form.send }}

表单页面无法呈现;追溯结束于:

File "project/populate_schedule.tpl.html", line 25, in block "page_content"
{% for field in day_values %}
TypeError: 'UnboundField' object is not iterable

n_days过去是硬编码的,字段被定义为没有__init__()函数的类变量,并且没有错误。如何以编程方式正确定义字段?我使用的是Python 2.7。

1 个答案:

答案 0 :(得分:1)

您需要将字段定义保留在类主体中,否则它们将不会在构建类时被提取。在初始化表单实例时,您可以将最大/最小条目值应用于字段列表。

class F(Form):

    day_values = FieldList(SelectField('Day Values',
                           coerce=str,
                           choices=[('low', 'Low'), ('medium', 'Medium'),
                                    ('high', 'High'), ('holiday', 'Holiday')]))
    send = SubmitField('Send Calendar')

    def __init__(self, *args, **kwargs):
        ndays = kwargs.pop('n_days')
        super(F, self).__init__(*args, **kwargs)
        self.day_values.min_entries = n_days
        self.day_values.max_entries = n_days

测试:

n_days = 4
f = F(n_days=n_days)
for x in range(n_days):
    f.day_values.append_entry()
for field in f.day_values:
    print field()
    print

输出:

<select id="day_values-0" name="day_values-0"><option value="low">Low</option><option value="medium">Medium</option><option value="high">High</option><option value="holiday">Holiday</option></select>

<select id="day_values-1" name="day_values-1"><option value="low">Low</option><option value="medium">Medium</option><option value="high">High</option><option value="holiday">Holiday</option></select>

<select id="day_values-2" name="day_values-2"><option value="low">Low</option><option value="medium">Medium</option><option value="high">High</option><option value="holiday">Holiday</option></select>

<select id="day_values-3" name="day_values-3"><option value="low">Low</option><option value="medium">Medium</option><option value="high">High</option><option value="holiday">Holiday</option></select>