Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't append_entry FieldList in Flask-wtf more than once

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.

like image 302
Mohammad Efazati Avatar asked Dec 26 '11 17:12

Mohammad Efazati


1 Answers

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?

like image 164
tkone Avatar answered Nov 03 '22 15:11

tkone