Reputation: 571
In Bottle, let's say I have a form with 10 inputs:
<form method="POST" action="/machine" enctype="multipart/form-data">
<input type="text" name="one" placeholder="one" required>
<input type="text" name="two" placeholder="two" required>
...
<input type="text" name="ten" placeholder="ten" required>
</form>
I want to then process all the potential inputs and don't want to do this statically by calling each one individually on the POST route (e.g. request.forms.get("one")).
Is there a way to process all the inputs in the form. I've been seeing request.params and request.query ...
@route('/machine', method='POST')
def machine_learn():
my_dict = dict(request.params)
return str(my_dict)
... but don't fully understand how I can use these to get all the input data as either a dictionary or list. When I use the above code, I get an empty dictionary
Any help is appreciated.
Upvotes: 4
Views: 1070
Reputation: 3907
Just for some additional help.
def merge_dicts(*args):
result = {}
for dictionary in args:
result.update(dictionary)
return result
class Api(object):
def __init__(self, user, request, option):
self.user = user
self.option = option
self.payload = merge_dicts(dict(request.forms), dict(request.query.decode()))
This can now take any form, or query post and combine them into a payload. The reason for the dict()
is because Bottle technically doesn't return a true dict, instead it is a FormsDict which does not have all the features of a dict in place. This makes that process simple, and turns everything into a dict.
Upvotes: 0
Reputation: 38932
request.forms
is an instance of bottle.FormsDict
class. 1
When you examine bottle.MultiDict
class with FormsDict subclasses, there is a allitems
method that can be used to retrieve a list containing tuples of field name and value. 2
form_items = request.forms.allitems()
Upvotes: 0
Reputation: 3703
request.forms
returns a Python Dictionary of all the inputs in the request.
So you can process the request dynamically like this
for key, value in request.forms.items():
print("For name " + key + ", the value is " + value)
Upvotes: 2