Is there a list of objects when sending requests in html?

Asked

Viewed 112 times

0

I’m developing a small web project in python(flask) for my own learning and came across something that I had never faced before.

I have a registration form where the fields of an information are dynamic, IE, via jquery I can create and delete fields because I do not know how many links the user will send. My question is the following, how do I receive the request of this form in my python code if I don’t know how many fields will be sent? Is there some sort of list in the request where I can create 'objects' to send everything together?

1 answer

1


As the documentation says here, the request.form is a ImmutableMultiDict who behaves like a dictionary.

You can use dictionary methods in it normally, such as request.form.keys(), request.form.items() or even iterate directly into it to see all the names used:

for chave in request.form:
    print(chave, request.form[chave])

Will print on the console everything that arrived from the form.

If your page sends multiple fields with the same name, even so you can pick up using getlist:

request.form.getlist(chave)

Will return a list with all values with that name.

  • Guess what is worth clarifying in other words - all form fields that on the HTML side have the equal "name" attribute are grouped in flask’s request.form, the contents of each field as an item in a list.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.