简体   繁体   English

如何在WTForms中呈现可变长度的FieldList?

[英]How can I render a FieldList of variable length in WTForms?

I'm trying to make a form to populate a calendar. 我正在尝试填写表格以填充日历。 The calendar can be five or seven days long, and I'd like to pass the number of days as an argument to the form. 日历可以是五到七天,我想将天数作为表单的参数传递。 My form is defined like so: 我的表单定义如下:

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')

It's rendered like so: 它的呈现方式如下:

<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 }}

The form page is failing to render; 表单页面无法呈现; the traceback ends with: 追溯结束于:

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 used to be hardcoded, and the fields were defined as class variables with no __init__() function, and there were no errors. n_days过去是硬编码的,并且这些字段被定义为没有__init__()函数的类变量,并且没有错误。 How do I correctly define the field programmatically? 如何正确地以编程方式定义字段? I am using Python 2.7. 我正在使用Python 2.7。

You need to leave the field definitions in the class body, otherwise they won't be picked up with the class is constructed. 您需要将字段定义保留在类主体中,否则将不会在构建类时对其进行选择。 When the form instance is being initialised you can apply the max/min entries values to the fieldlist. 初始化表单实例时,可以将最大/最小条目值应用于字段列表。

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

Test: 测试:

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

Output: 输出:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM