如何从复选框表单Django获取值数组 [英] How to get array of values from checkbox form Django
本文介绍了如何从复选框表单Django获取值数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的HTML表单:
I have HTML form like this:
<p>Models Sizes IDs:</p>
<input type="checkbox" name="model_size_ids[]" value="1">XS</input>
<input type="checkbox" name="model_size_ids[]" value="2">S</input>
<input type="checkbox" name="model_size_ids[]" value="3">M</input>
<input type="checkbox" name="model_size_ids[]" value="4">L</input>
<button>Submit</button>
我正在尝试在我的 View
中接收服务器端的一组检查值:
I'm trying to receive an array of checked values on server side in my View
:
size_ids = request.data['model_size_ids[]']
但是,我只能提取一个值和最后一个值.所以如果我检查2-3个值复选框形式,我在视图中仅收到最后一个值.我还尝试命名不带括号的输入字段,结果是相同的.谁能告诉我,我该如何解决?谢谢!
But, I can extract only one and the last value. So if I check 2-3 values in checkbox form, I receive only last value in my view. I also tried to name input field without braсkets and the result was the same. Can anybody tell me, how can I solve that? Thanks!
推荐答案
使用getlist方法获取选定选项的列表
Use the getlist method for geting the list of selected choices
request.POST.getlist('model_size_ids[]')
这篇关于如何从复选框表单Django获取值数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文