WTForms fieldlist'StringField'对象不是可迭代的错误

时间:2017-08-24 16:23:26

标签: python iteration wtforms flask-wtforms

我只有在以包含其他字段的形式定义字段时才会收到此错误 - 但是当它以自己的形式定义时,不会生成此错误并且它可以正常工作。

首先,我将展示成功的方法:

class TrackForm(Form):
    name = StringField('Name')
    start = StringField('Start')
    end = StringField('End')

class Merge(Form):
    item_description = FieldList(FormField(TrackForm), min_entries=1, max_entries=32)

view.py

@app.route('/test', methods=['GET', 'POST'])
def test():
        form=Merge()
        return render_template(
        'test.html', title='test', form=form)

模板/的test.html:

<form method="POST" >
  {{ form.hidden_tag() }}
  <table id="fruitTable" class="table">
    <tr><td><h3>Tracks</h3></td></tr>
    {% set counter = 0 %}
    {% for track in form.item_description %}
      <tr>
        {{ track.hidden_tag() }}
        {% set counter = counter + 1%}
        {% for field in track if field.widget.input_type != 'hidden' %}
          {{ render_field_oneline(field) }}
        {% endfor %}
        {% if counter > 1 %}
          <td>
            <button class="btn" type="button" onClick="removeTrack()"><i class="icon-black icon-minus"></i></button>
          </td>
        {% endif  %}
      </tr>
    {% endfor %}
      <tr><td></td><td><button class="btn" type="button" onClick="addTrack()"><i class="icon-black icon-plus"></i></button></td></tr>
  </table>
<input class="btn btn-primary" style="margin-left:300px;"type="submit" value="Submit" />
</form>
<script src="static/js/populateselect.js" type="text/javascript"></script>
{% endblock %}

但是,当我尝试将item_description字段移动到包含其他字段的实时表单时,我会遇到问题。 以下是不起作用并产生错误:

class TrackForm(Form):
    name = StringField('Name')
    start = StringField('Start')
    end = StringField('End')

class SendForm(Form):
        item_description = FieldList(FormField(TrackForm), min_entries=1, max_entries=32)
        rai_number = StringField('rai_number', validators=[DataRequired(), Regexp('^RA[0-9]{5}$', message='Asset number must contain 7 characters- "RA" followed by 5 digits')])
        spool_number = StringField('spool_number', validators=[DataRequired(), Regexp('^[a-zA-Z0-9]{1,19}$', message='You have entered too many numbers/digits')], filters = [lambda x: x or None])
        carrier_format = SelectField('carrier_format', choices=carrier_choices, validators=[DataRequired()], filters = [lambda x: x or None])
        physical_location = SelectField('physical_location', choices=physical_location_choices, validators=[DataRequired()], filters = [lambda x: x or None])
        programme_sub_category = SelectField('programme_sub_category', choices=sub_category_choices, filters = [lambda x: x or None])
        programme_category = SelectField('programme_category', choices=[('',''),('Live Music', 'Live Music'), ('ROT', 'ROT'), ('Unpublished', 'Unpublished'), ('Pre-Rec', 'Pre-Rec'), ('Interview', 'Interview')], filters = [lambda x: x or None])
        programme_title = StringField('programme_title', filters = [lambda x: x or None])

views.py

@app.route('/test', methods=['GET', 'POST'])
def test():
        form=SendForm()
        return render_template(
        'test.html', title='test', form=form)

模板保持不变。

这是错误:

{% for track in form.item_description %}
[Thu Aug 24 16:55:20.142971 2017] [:error] [pid 56022] [remote 10.100.48.53:124] TypeError: 'StringField' object is not iterable

我不明白为什么将item_description字段移动到单独的表单会产生此问题?

0 个答案:

没有答案