How to get all items in multiple lists in a bottle?

I have a form that allows me to add to a list using js. I want to be able to send all the data in this list to my bottle server, but I can’t get any data there. How to get all elements in my report for publication on server.py? How to access this data after publication?

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 multimedia through a text field, button and JS; so I want to get all the values ​​(or all 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")
    });
+4
2

; :

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

request.forms getall. request.forms MultiDict, . getall - , MultiDict:

for choice in all_selected:
    # do something with choice

, :

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

, .getall. , .

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

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>

stdout :

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
[]

, .

+2

Source: https://habr.com/ru/post/1532985/


All Articles