Accessing POST params with same name in python - 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."

Related

How to get a value from <input type="hidden" in Python

Hi everyone as summarized in the title I want to get values from an input type="hidden" but it returns 'None'. Does it exist a method to get it?
Thanks in advance!
What I expect is to get values from "PaReq" and "MD"
<input type="hidden" name="PaReq" value="eNpVUstuwjAQ/JWID4gfOA+jxRJtKpVKpAjooUfXOJCKJOAkJfx97RCgtXzYmd0dr2YNm73ROllr1RotYKHrWu60l2+nowmLWRBRHhJG4yAidCRgOVvpk4Afbeq8KgXxsU8B3aBtN2ovy0aAVKeneSoCGgZhBGiAUGgzTwYWY0oDjiMM6EpDKQst1gdZeG+yANRDUFVbNuYixrEtvAFozUHsm+ZYTxA6n89+bbu+ZeGryja6JKDHMMvWRbUV6/KtSJMdTjfq8r757NJkzmzcLXYzd6aAXAVsZaMFxYQTggOPjCfM3hhQz4Ms3BTi5WPlcexjO9bAwNE9NLsCjl3mLwPWY6NLdRE8smJ3BLo7VqW2FdbLewzoMfbzq3NUNdakLxKTmI2zSEYqzDgPGY/DQGZZKDPnc1/kFHNrE2U46iUdAORk0LBCNKzaRv++wC/NTqrF">
<input type="hidden" name="MD" value="djRZa0UxelQrcVc5Z0FySWJxdlg0Zz09IVoFvEENTvjwOCe_O4c2oKjmkJX--9pXJipeKnSw3qmylmi9CRNpM_VB7PE6ThiwlR89Siu7qEpUkj7rdnVJLLuXGbViF6O9akJ4lnNqj_kWEveLghKDyAxonmcKg4hSia1VyORwlDlWJ3g9wWjU0bwVvzohyEeTzagDF3f8ggFYIYWgHiJ8ceDr0XSwoFYYxOmlb09yuyCP6LBZd-k3ED8KUo7B5pomsQF3M4RFBtS_dQdBw9wGy9R1cACkzAL0w7BvwNqw5vm3xOyFL7HUGlFbhDOPpFJ8LhEsDQwr2zptI4VBhDt0rYmJkt2hAlzU2OTPGyuvXD-8zWBGI5jcwlqCcCXPlsjMzKCSnfJuFGpxkAFIkqdEGBBKJL7Lqag06OKTO7x2HQaNYmBTKVz1uYG9FE3DUl06MjH7LzBlU1mj7-Bx_YTTuA">
This is my code:
script = s.get('https://www.slamjam.com/on/demandware.store/Sites-slamjam-Site/en_IT/Adyen-Adyen3D',headers=headers)
bs_3d = BeautifulSoup(script_3d.text,"html.parser")
hidden_tags = bs_3d.find_all("input", type="hidden")
for tag in hidden_tags:
print(tag.value)
you should use tag["value"] instead of tag.value.
from documentation
Attributes
A tag may have any number of attributes. The tag <b id="boldest"> has an attribute “id” whose value is “boldest”. You can access a tag’s attributes by treating the tag like a dictionary:
>>>tag['id']
u'boldest'

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>

How to get array of values from checkbox form Django

I have HTML form like this:
<p>Models Sizes IDs:</p>
<input type="checkbox" name="model_size_ids[]" value="1">XS</input>
<input type="checkbox" name="model_size_ids[]" value="2">S</input>
<input type="checkbox" name="model_size_ids[]" value="3">M</input>
<input type="checkbox" name="model_size_ids[]" value="4">L</input>
<button>Submit</button>
I'm trying to receive an array of checked values on server side in my View:
size_ids = request.data['model_size_ids[]']
But, I can extract only one and the last value. So if I check 2-3 values in
checkbox form, I receive only last value in my view. I also tried to name input field without braсkets and the result was the same. Can anybody tell me, how can I solve that?
Thanks!
Use the getlist method for geting the list of selected choices
request.POST.getlist('model_size_ids[]')

Parse POST data containing square brackets in 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.

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.

Categories