Cant append_entry FieldList in Flask-wtf more than once

2024/11/15 14:08:48

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.

Answer

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://en.xdnf.cn/q/71781.html

Related Q&A

What is the best way to use python code from Scala (or Java)? [duplicate]

This question already has answers here:Closed 11 years ago.Possible Duplicate:Java Python Integration There is some code written in Python and I need to use it from Scala. The code uses some native C.…

Pandas groupby week given a datetime column

Lets say I have the following data sample:df = pd.DataFrame({date:[2011-01-01,2011-01-02,2011-01-03,2011-01-04,2011-01-05,2011-01-06,2011-01-07,2011-01-08,2011-01-09,2011-12-30,2011-12-31],revenue:[5,3…

Django form to indicate input type

Another basic question Im afraid which Im struggling with. Ive been through the various Django documentation pages and also search this site. The only thing I have found on here was back in 2013 which…

run multi command in the same jupyter cells

Im trying to display 2 output of 2 lines in the same time, I use Panda library and it seems like it display only the output of second line:import pandas as pd data = {"state": ["Ohio&quo…

Pandas how to get rows with consecutive dates and sales more than 1000?

I have a data frame called df: Date Sales 01/01/2020 812 02/01/2020 981 03/01/2020 923 04/01/2020 1033 05/01/2020 988 ... ...How can I get the first occurrence of 7 conse…

Use Python alongside C# in Windows UWP app

I started writing an application in Python, but I now want to switch to C# and UWP. I know that you cannot write a UWP app in Python, but I am trying to see if I can write some code in Python and acces…

How do you go from a sip.voidptr (QImage.constBits()) to a ctypes void or char pointer?

Im using python and of course you cant loop through every pixel of a large image very quickly, so I defer to a C DLL.I want to do something like this:img = QImage("myimage.png").constBits() i…

Just returning the text of elements in xpath (python / lxml)

I have an XML structure like this:mytree = """ <path><to><nodes><info>1</info><info>2</info><info>3</info></nodes></to> …

Python function parameter: tuple/list

My function expects a list or a tuple as a parameter. It doesnt really care which it is, all it does is pass it to another function that accepts either a list or tuple:def func(arg): # arg is tuple or …

Python binding for C++ operator overloading

I have a class similar to the following:class A {vector<double> v;double& x(int i) { return v[2*i]; }double& y(int i) { return v[2*i+1]; }double x(int i) const { return v[2*i]; }double y(…