Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending a form array to Flask

Tags:

python

html

flask

I have an HTML form with multiple inputs named like this:

<input name="hello[]" type="text" /> <input name="hello[]" type="text" /> <input name="hello[]" type="text" /> 

In PHP you get this as an array but is it the same way in Python, using Flask?

I have tried this:

hello = request.form['hello']  print(hello) 

But that did not work, I got a 400 Bad Request:

Bad Request  The browser (or proxy) sent a request that this server could not understand. 

How do I do it in Flask?

like image 905
rablentain Avatar asked Jul 17 '14 16:07

rablentain


People also ask

How do you collect form data in flask?

You can get form data from Flask's request object with the form attribute: from flask import Flask, request app = Flask(__name__) @app. route('/', methods=['GET', 'POST']) def index(): data = request.

What is ImmutableMultiDict?

In this article, we will see how to get data from ImmutableMultiDict in the flask. It is a type of Dictionary in which a single key can have different values. It is used because some elements have multiple values for the same key and it saves the multiple values of a key in form of a list.


2 Answers

You are following a PHP convention of adding brackets to the field names. It's not a web standard, but because PHP supports it out of the box it is popular; Ruby on Rails also uses it.

If you do use that convention, to get the POST data on the Flask side you need to include the square brackets in the field name. You can retrieve all values of the list using MultiDict.getlist():

hello = request.form.getlist('hello[]') 

You don't have to use the [] convention at all, of course. Not appending the [] to the hello name will work perfectly fine, at which point you'd use request.form.getlist('hello') in Flask.

like image 169
Martijn Pieters Avatar answered Sep 23 '22 06:09

Martijn Pieters


I written a parse function which supports multidimensional dict:php_post=parse_multi_form(request.form)

def parse_multi_form(form):     data = {}     for url_k in form:         v = form[url_k]         ks = []         while url_k:             if '[' in url_k:                 k, r = url_k.split('[', 1)                 ks.append(k)                 if r[0] == ']':                     ks.append('')                 url_k = r.replace(']', '', 1)             else:                 ks.append(url_k)                 break         sub_data = data         for i, k in enumerate(ks):             if k.isdigit():                 k = int(k)             if i+1 < len(ks):                 if not isinstance(sub_data, dict):                     break                 if k in sub_data:                     sub_data = sub_data[k]                 else:                     sub_data[k] = {}                     sub_data = sub_data[k]             else:                 if isinstance(sub_data, dict):                     sub_data[k] = v      return data 

Usage:

>>> request.form={"a[0][name]": "ahui", "a[0][sex]": "female", "a[1][name]": "bhui", "a[1][sex]": "male"} >>> parse_multi_form(request.form) {'a': {0: {'name': 'ahui', 'sex': 'female'}, 1: {'name': 'bhui', 'sex': 'male'}}} 

Warnning: It does not support list,e.g. a[][0]=1&a[][0]=2, it may make programmer to be confused. Either a=[[1,2]] or a[[1],[2]] is too hard to choose.

So I suggest use dict to replace list:

<input name="hello[0]" type="text" /> <input name="hello[1]" type="text" /> 

If you still want to post complex data, I suggest you use application/json

like image 40
ahuigo Avatar answered Sep 19 '22 06:09

ahuigo