Question

I have a form that allows me to add to a multi list using js. I want to be able to post all the data in that list to my bottle server, but I am not able to get any of the data in there. How do I get all the items in my statement to be posted to server.py? How do I access this post data once it is posted?

Relevant code:

server.py:

@bottle.route('/saveList', method='POST')
def save_list():
    forms = bottle.request.get('the_list')
    print forms # returns 'None'
    return bottle.redirect('/updatelist') # just redirects to the same page with a new list

list.tpl

 <select multiple="multiple" id="the_list" name="the_list">
     %for item in my_ list:
     <option>{{item}}</option>
     %end
 </select>

EDIT:

I am trying to get the whole list, not just the selected values. The user adds to the multi by way of textfield, button, and JS; so I want to get all the values (or all the new values).

EDIT 2:

I used the answers provided along with some js to get the desired result:

$('.add_to_the_list').click(function (e) {
...
var new_item = $('<option>', {
                value: new_item_str,
                text: new_item_str,
                class: "new_item" // the money-maker!
            });
...

function selectAllNewItem(selectBoxId) {
    selectBox = document.getElementById(selectBoxId);
    for (var i = 0; i < selectBox.options.length; i++) {
        if (selectBox.options[i].className === "new_item") { // BOOM!
            selectBox.options[i].selected = true;
        }
    }
}

...
    $('#submit_list').click(function (e) {
        selectAllNewBG("the_list")
    });
Was it helpful?

Solution

You were close; just try this instead:

all_selected = bottle.request.forms.getall('the_list')

You'll want to use request.forms and getall. request.forms returns a MultiDict, which is the appropriate data structure for storing multiple selected options. getall is how you retrieve the list of values from a MultiDict:

for choice in all_selected:
    # do something with choice

or, more simply:

for selected in bottle.request.forms.getall('the_list'):
    # do something with selected

OTHER TIPS

To get multiple values back use .getall. Here is the code I was able use this with.

import bottle

@bottle.route('/saveList', method='POST')
def save_list():
    forms = bottle.request.POST.getall('the_list')
    print forms 
    return bottle.redirect('/updatelist')

@bottle.route('/updatelist')
@bottle.view('index')
def index():
    return {}

bottle.run()

The HTML

<html>
    <body>
        <form method="post" action="http://localhost:8080/saveList">
             <select multiple="multiple" id="the_list" name="the_list">
                 <option value="1">Item 1</option>
                 <option value="2">Item 2</option>
                 <option value="3">Item 3</option>
             </select>
            <input type="submit" />
         </form>
    </body>
</html>

The output to stdout looks like:

127.0.0.1 - - [22/Mar/2014 13:36:58] "GET /updatelist HTTP/1.1" 200 366
['1', '2']
127.0.0.1 - - [22/Mar/2014 13:37:00] "POST /saveList HTTP/1.1" 303 0
127.0.0.1 - - [22/Mar/2014 13:37:00] "GET /updatelist HTTP/1.1" 200 366
[]

First time selected two objects, second time didn't select any.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top