I have a form with flask-wtf for uploading images, also file field can be
multiple fields.
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 the form, append_entry doesn’t work. It only appends one more field.
Again I click on add_upload, but after refresh I have again only one field (not two).
How can I fix this? There is no error, I think, because enctype wtform forgets how many fields I have to add more.
You call to
append_entryis missing it’s data.From the Documentation:
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 whatform.attachment.entrieslooks like. Does it contain values? What happens when you iterate through those values?