Search code examples
pythonbottle

Processing Multiple POST Input Values with Python Bottle


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.


Solution

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