gpt4 book ai didi

python - 如何获取瓶中多个列表中的所有项目?

转载 作者:太空狗 更新时间:2023-10-30 01:01:45 25 4
gpt4 key购买 nike

我有一个允许我使用 js 添加到多列表的表单。我希望能够将该列表中的所有数据发布到我的 Bottle 服务器,但我无法从中获取任何数据。如何让我的声明中的所有项目都发布到 server.py?发布后如何访问此发布数据?

相关代码:

服务器.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

列表.tpl

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

编辑:

我正在尝试获取整个列表,而不仅仅是选定的值。用户通过textfield、button、JS的方式添加到multi;所以我想获取所有值(或所有新值)。

编辑 2:

我使用了提供的答案和一些 js 来获得所需的结果:

$('.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")
});

最佳答案

你很接近;试试这个:

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

您需要使用 request.formsgetall . 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

关于python - 如何获取瓶中多个列表中的所有项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22579797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com