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")
});
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.
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.
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
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With