request.POST.getlist() 获取不到checkbox的值,请大神帮忙,两天了实在找不到原因
前段: <table align="center" border="3"> <tr>
<th>序号</th>
<th>设备名</th>
<th>型号</th>
<th>厂家</th>
<th>序列号</th>
<th>存放地址</th>
<th>进厂日期</th>
<th>保固日期</th>
</tr>
{% for i in equipment %}
<tr>
<td >
<input type="checkbox" name="checkbox_list" value='{{i.3}}'>{{i.3}}
</td>
<td>{{i.0}}</td>
<td>{{i.1}}</td>
<td>{{i.2}}</td>
<td>{{i.3}}</td>
<td>{{i.4}}</td>
<td>{{i.5}}</td>
<td>{{i.6}}</td>
</tr>
{% endfor %}
</table>
</form>
<form action="/delete_data" method="post">
{% csrf_token %}
<table align="center">
<tr>
<td>
<input type="submit" value="删除" >
</td>
</table>
后端: def delete_data(request):
check_book_list=request.POST.getlist('checkbox_list')
print(check_book_list)
if check_book_list:
for i in check_book_list:
equipment.objects.filter(equipment_serial_number=check_book_list).delete()
return redirect('/index')
评论 1
如果不是基于ajax的提交而是基于form表单提交,那么你的input[type='checkebox']需要放到form标签里面才可以。
大神,感谢!一语惊醒!可以写邮件给您请教问题吗?
可以,多来我博客看看。
感谢大神!