Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all items in multi list in bottle?

Tags:

python

bottle

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")
    });
like image 716
Jeff Avatar asked Mar 22 '14 15:03

Jeff


People also ask

How to select all the products in a list?

To select all the Products, you'll need to tick all the checkbox in Product Selection. And this can be tedious, so you can also create a 4th module called "All Product" without any list, but create 1 boolean line item called "Select". So now you can slightly modify the line items in the 3rd module created above, i.e.

How can I add multiple elements to my ArrayList simultaneously?

I needed a way to add multiple elements to my ArrayList simultaneously. How can we do that without a loop? We can add all items from another collection to an ArrayList using addAll (). First, we would have to define a new list using Arrays.asList (). Then, we can call addAll () on the original list.

How to select multiple values from an existing value in Excel?

There is no way you can do this with Excel in-built features. The only way is to use a VBA code, which runs whenever you make a selection and adds the selected value to the existing value. Watch Video – How to Select Multiple Items from an Excel Drop Down List

How to add elements to an immutable list in Java?

Then, we can call addAll () on the original list. We can use the Collections class, which contains many static methods to operate on collections. Using addAll (), we can add any number of elements into our collection. As of Java 9, we can use List.of () to instantiate an immutable list. So, if this fits your use case, feel free to use this.


2 Answers

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
like image 162
ron rothman Avatar answered Oct 17 '22 06:10

ron rothman


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.

like image 43
CasualDemon Avatar answered Oct 17 '22 07:10

CasualDemon