Parse POST data containing square brackets in Python? - python

I'm writing a python webapp (on Google AppEngine, using webob) that should process a form containing a list of addresses.
To simplify, lets say the form contains these inputs:
<input type="hidden" name="address[]" value="1" />
<input type="hidden" name="address[]" value="2" />
<input type="hidden" name="address[]" value="3" />
<input type="hidden" name="address[]" value="4" />
Now, Rails\PHP would parse this to a single 'address' value containing a list [1,2,3,4].
Is there a simple way to do this in Python?

from inside a RequestHandler method, self.request.params.getall('address[]') will return a list of the values. The [] is of no significance though, the fields could just as easily be named 'address'.

WebOb uses something called a MultiDict. self.request.POST will take an appropriately form encoded request and return a MultiDict.
From this MultiDict you can call .getall() with the name of a key, in this case your key would be "address[]".
To get a list of entries you would call:
self.request.POST.getall('address[]')
It is not required that you use address[] as the name as MultiDict doesn't use that to identify whether a key can exist multiple times or not.

Related

multiple inputs of the same name in an html form

I have some html code that contains a form. When submitted it sends a post request to a server. The form has various inputs, each with a name and a value, however there are some inputs with the same name.
Here is an example of what I mean:
<form action="http://example.com" method="post">
<input name="name" value="val">
<input name="name" value="val">
<input type="submit">
</form>
First, I am confused as to how there can be two values of the same name. Please note that I have tried removing one of the instances of <input name="name" value="val"> however this returns an error so it seems that both instances are needed.
Second, I am trying to convert this to a python request using the request library.
I have the following request:
requests.get(url = URL, params = PARAMS).json()
Where PARAMS is a dictionary of the various inputs. For example:
PARAMS = {'name':"val"}
However, being a dictionary, I can't have multiple instances of the same value. How do I make this work? Thanks
If there are duplicated names then the values will be in an array with the name. The demo below sends to a live test server and the response is directed to an <iframe>
<form action="https://httpbin.org/post" method="post" target="response">
<input name="name" value="val">
<input name="name" value="val">
<input type="submit">
</form>
<iframe name='response'></iframe>
What you are looking for is to use input arrays. With them, you can have many inputs sharing the same name, and in the server side, the data will be treated as an array. So the HTML would be:
<form action="http://example.com" method="post">
<input name="name[]" value="val1">
<input name="name[]" value="val2">
<input type="submit">
</form>

Get list of input names and values from HTML field set in Django

Using Django I'm looking for a good way to get the input values and names of type=hidden from a template HTML. I understand the request.POST.getlist method would return a list of all inputs with the same name value. But I actually need to get all the names as well as the values. i.e.,
HTML Code:
<fieldset class="submit">
<input type="hidden" name="testName" value="0" id="processed-input" />
<input type="submit" name="submit" value="Rotate" class="submit" />
</fieldset>
And I need to get a list of related names and values. For this case, testName:0.
Any help would be appreciated! Thanks!
That's what request.POST is: a mapping of all names to values.

Retrieving form field attribute in django

I have an input text field in my form which has an important new attribute added via JavaScript. The attribute name is fieldid.
Is there a way to retrieve this attribute and its value from the view after the form is summited?
Attributes on form elements are not passed on to the server when forms are submitted; only input element values are.
The way to add extra information to be sent back to a server on a form submit is by adding additional <input /> elements to your form, specifically <input type="hidden" /> elements.
E.g.
<input type="hidden" name="foobar" value="spam and eggs" />
adds a new field foobar to your form data with the value spam and eggs.

self.request.form not iterable?

Hello I am making a form in HTML and python and I am having a problem with processing the arguments
the problem comes in this section
<form method="POST">
<input type="checkbox" name="brands" value="1" />
<input type="checkbox" name="brands" value="2" />
</form>
and in the python I am using a self.request.form object to retrieve the arguments
the problem is if I do something like
for b in brands:
print b
it will just print out 1 even if both of them are in the self.request.form object
USING Werkzeug Framework
ANSWERED:
I found you can retrieve a list of the same named inputs using this syntax
self.request.form.getlist('brands')
I found you can retrieve a list of the same named inputs using this syntax
self.request.form.getlist('brands')

Accessing POST params with same name in python

I need to get values of these check boxes with same name through HTTP "POST".
<input type="checkbox" id="dde" name="dept[]" value="dde"/>
<input type="checkbox" id="dre" name="dept[]" value="dre"/>
<input type="checkbox" id="iid" name="dept[]" value="iid"/>
How to get these values in python using self.request.get() method?
You can use request.get_all().
According to the docs it "Returns a list of values of all of the query (URL) or POST arguments with the given name, possibly an empty list."

Categories