如何获取 Django request.POST 中所有选定复选框的值? [英] How do I get the values of all selected checkboxes in a Django request.POST?
本文介绍了如何获取 Django request.POST 中所有选定复选框的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一组复选框,例如
<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" />
如果选择了多个,我如何在 view.py 中访问这些?
How do I access these in the view.py if more than one is selected?
我试过了
request.POST['checks']
但这只能给我最后的价值.我想要的是在列表中选择的所有内容,例如1,3,4
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
谢谢
推荐答案
试试这个:
some_var = request.POST.getlist('checks')
some_var
将包含 [1,3,4]
(那些被检查的值)
some_var
will contain [1,3,4]
(those values that were checked)
这篇关于如何获取 Django request.POST 中所有选定复选框的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文