如何在Django request.POST中获取所有选中的复选框的值? [英] How do I get the values of all selected checkboxes in a Django request.POST?

查看:155
本文介绍了如何在Django request.POST中获取所有选中的复选框的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我有一个复选框,例如

 < input type =checkboxname =checking [] value =1/> 
< input type =checkboxname =checking []value =2/>
< input type =checkboxname =checking []value =3/>
< input type =checkboxname =checking []value =4/>

如果选择了多个选项,我如何在view.py中访问这些?



我尝试过

  request.POST ['check'] 

但这只能给我最后一个值。我想要的是在列表中选择的所有内容1,3,4



谢谢

解决方案

/ p>

  some_var = request.POST.getlist('checking')

some_var 将包含 [1,3,4] (那些被检查的值)


Hi I have an array of checkboxes e.g.

<input type="checkbox" name="checks[]" value="1" />
<input type="checkbox" name="checks[]" value="2" />
<input type="checkbox" name="checks[]" value="3" />
<input type="checkbox" name="checks[]" value="4" />

How do I access these in the view.py if more than one is selected?

I have tried

request.POST['checks']

but that only gives me the last value. What I want is all the ones that have been selected in a list e.g. 1,3,4

Thanks

解决方案

Try this:

some_var = request.POST.getlist('checks')

some_var will contain [1,3,4] (those values that were checked)

这篇关于如何在Django request.POST中获取所有选中的复选框的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆