django获取多个checkbox复选框post的数组数据

2022-05-03 00:00:00 多个 数组 复选框

有多个复选框需要在django中获取post的值,并存储在数组中,下面的代码演示了你应该如何做。
html代码如下:

<input type="checkbox" name="chk" value="1">
<input type="checkbox" name="chk"  value="1">
<input type="checkbox" name="chk"  value="1">
<input type="checkbox" name="chk"  value="1">

在django代码中获取name=chk的复选框

values = request.POST.getlist('chk')

得到的结果形式如下
['1','2']

相关文章