Question

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

Was it helpful?

Solution

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top