Python 列表始终为空

Python 列表始终为空,python,flask,Python,Flask,我正在尝试添加复选框,以便可以从数据库中删除多条记录。我给多个输入相同的名称,但是request.form.getlist返回一个空列表,即使选中了一些框 {%用于订单%} {%endfor%} 这把我撞倒了好几个小时!! 希望这篇文章能帮助人们节省时间 当您提供多个输入时,您的输入类型将隐式更改为数组。 这意味着您必须从数组中提取结果。 通过在输入变量“[]”的名称末尾添加这两个字符来实现: 在您的情况下,我们得到: 订单=(request.form.getlist('select_按钮[]

我正在尝试添加复选框,以便可以从数据库中删除多条记录。我给多个输入相同的
名称
,但是
request.form.getlist
返回一个空列表,即使选中了一些框

{%用于订单%}
{%endfor%}

这把我撞倒了好几个小时!! 希望这篇文章能帮助人们节省时间

当您提供多个输入时,您的输入类型将隐式更改为数组。 这意味着您必须从数组中提取结果。 通过在输入变量“[]”的名称末尾添加这两个字符来实现:

在您的情况下,我们得到: 订单=(request.form.getlist('select_按钮[]'))


fullstack示例:

==>在html中使用引导

<select class="selectpicker" multiple title="input list" id="inputlist">
<option>input1</option>
<option>input2</option>
<option>input3</option>
</select>
<small>Click Go! to proceed : </small>
<button class="myConfirmButton btn btn-primary">Go!</button>
<small><div id="myFeedback"></div></small>

输入1
输入2
输入3
点击开始!进行:
走!
==>在jquery/javascript中

<script>
$(document).ready(function(){
$(".myConfirmButton").click(function(){
// this line creates the array of all the checked inputs 
var values = $('#listinput').val()
  $.ajax({
    url:"/processlist",
    dataType:'html',
    type:'post',
    contentType: 'application/x-www-form-urlencoded',
    data : {
       listinput: values
    },
    success: function( data ){
    $('#myFeedback').html( data );
  },
    error: function( errorThrown ){
    $('#myFeedback').html( errorThrown );
  }
    });
});
});
</script>

$(文档).ready(函数(){
$(“.myconfirbutton”)。单击(函数(){
//此行创建所有选中输入的数组
var值=$('#listinput').val()
$.ajax({
url:“/processlist”,
数据类型:'html',
类型:'post',
contentType:'application/x-www-form-urlencoded',
数据:{
listinput:值
},
成功:功能(数据){
$('#myFeedback').html(数据);
},
错误:函数(错误抛出){
$('#myFeedback').html(error抛出);
}
});
});
});
==>在python代码中

@app.route("/processlist",methods=['GET', 'POST'])
def processlist():
  myinput=request.form.getlist('listinput[]') # here you add [] at the end
  return '<br>'.join(myinput) # shows the total input
@app.route(“/processlist”,方法=['GET','POST']))
def processlist():
myinput=request.form.getlist('listinput[])#这里您在末尾添加了[]
返回“
”。join(myinput)#显示总输入
@app.route("/processlist",methods=['GET', 'POST'])
def processlist():
  myinput=request.form.getlist('listinput[]') # here you add [] at the end
  return '<br>'.join(myinput) # shows the total input