self.request.form not iterable? - python

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')

Related

bootstrap tags input tag value is not submitting with form

Can someone explain what is going on here? I don't understand why the values that I submit in the tag input field are not being submitted with the form. I've tried this with both examples
and neither will send the values correctly.
I am using bootstrap4-tagsinput
HTML CODE:
<form class="needs-validation" action="/archive" enctype="multipart/form-data" method="POST">
<div class="form-group">
<label>Solution Name</label>
<input name="solution_name" type="text" class="form-control" required>
<label>Vendor Name</label>
<input name="vendor_name" type="text" class="form-control" required>
</div>
<div class="form-group">
<label>Attachment</label>
<input name="file" type="file" class="form-control-file" required>
</div>
<div class="form-group">
<label>Tags</label>
<input class="form-select" name="tags" data-role="tagsinput">
</input>
</div>
<button type="submit" value="upload" class="btn btn-primary">Submit</button>
</form>
Server
#review_archive_host.route('/archive', methods=["GET", "POST"])
#login_required
def archives():
if "review_archive" not in session['u']['flags']:
flash("You do not have permissions to access that feature/page")
return redirect(get_redirect_url())
#archive search page
if request.method == "POST":
#create a new archive record
d = request.form.to_dict(flat=True) or {}
return d
Example form and response:
response:
{
"solution_name": "asdfs",
"tags": "",
"vendor_name": "asfsd"
}
In the documentation of the link you provided informs that this puglin was designed for Bootstrap 2.3.2 and 3. By the tag of your question, I saw that you are using 4.
And others people having issues using it on Bootstrap 4.
I uploaded a small example based on your form and method for Github, but using version 3 of Bootstrap and it worked as expected.
An alternative could be this. They fix the compatibility problem with bootstrap 4 based on the plugin you used initially
EDIT:
After your answer's update using Nodws/bootstrap4-tagsinput I made a small test using this plugin and I was able to reproduce the problem.
When analyzing the request.form sent, I noticed that with this version of the plugin the key tags are coming in duplicate.
ImmutableMultiDict([('vendor_name', u'vendor'), ('solution_name', u'solution'), ('tags', u''), ('tags', u'tag1,tag2')])
Because of that you dont get any value when your route parser the form to dict.
I replicate your code with this another version of plugin and now is working as expected.
The complete example continues on my Github.
The plugin in the documentation is for bootstrap 2.3.2 or 3 and would not work for bootstrap 4.
In order to use that plugin, you could downgrade your bootstrap to version 3.
An alternative would be the Bootstrap 4 Tag Input Plugin With jQuery - https://www.jqueryscript.net/form/Bootstrap-4-Tag-Input-Plugin-jQuery.html

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>

Receive dynamic input (name[]) in Google App Engine

How to access (in Google App Engine) the input that is created dynamically:
<form action="/add" method="post">
<input type="text" name="line[]">
<input type="text" name="line[]">
<input type=submit">
I tried to access it via:
for i in self.request.get('line[]'):
self.response.out.write(i)
#this only gives first value
or
self.response.out.write(self.request.get('line[]')[1])
#this gives index out of range.
Ok, i got it, i supposed to use self.request.get_all('line[]')

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