can't append_entry FieldList in Flask-wtf more than one

你。 提交于 2019-12-21 15:19:13

问题


I have form with flask-wtf for upload some image, also file field can be multiple

my form:

 class ComposeForm(Form):
     attachment = FieldList(FileField(_('file')), _('attachment'))
     add_upload = SubmitField(_('Add upload'))

my view:

  if form.validate_on_submit():
         if form.add_upload.data:
             form.attachment.append_entry()
             return render_template('mailbox/compose.html', form=form)
         else:
             form.attachment.append_entry()

my template :

<form method="POST" enctype="multipart/form-data" action=".">
                {% for field in form %}
                {{field}}
                {% endfor %}
</div>

when i use enctype="multipart/form-data" in form append_entry doesn't work, only append one more field again click on add_upload but after refresh i have agen only one field ( not two )

how i can fix this? there is no error, i think because of enctype wtform forget how many field i have to add more :D


回答1:


You call to append_entry is missing it's data.

From the Documentation:

append_entry([data])

Create a new entry with optional default data.

Entries added in this way will not receive formdata however, and can only receive object data.

If you're trying to get the data that was submitted on the form, you might try to use pop_entry. Or at least doing some debugging and seeing what form.attachment.entries looks like. Does it contain values? What happens when you iterate through those values?



来源:https://stackoverflow.com/questions/8637831/cant-append-entry-fieldlist-in-flask-wtf-more-than-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!