这是后端的代码
def sr(request):
bd = request.GET.get('b')
if bd:
obj = models.brown_plant_hopper_new.objects.filter(id=bd)
return render(request, 'sr_list3.html', {"obj": obj})
这是前端页面的代码
<form method="get">
<table class="table table-bordered">
<thead>
<tr>
<th>
<button class="btn btn-default" type="submit">比对button>
th>
<th>IDth>
<th>年份th>
<th>月份th>
<th>姓名th>
<th>实验环境th>
tr>
thead>
<tbody>
<tr>
<td><input type="checkbox" name="b" value="{{ item.id }}">td>
<td>{{ item.id }}td>
<td>{{ item.year }}td>
<td>{{ item.month }}td>
<td>{{ item.name }}td>
<td>{{ item.plot }}td>
tr>
tbody>
table>
form>
在网上找答案说要将name="b"改为name="b[ ]",使用数组。但是我尝试过后发现页面并不跳转到'sr_list3.html'页面,而是停留在当前页面,url上显示b%5B%5D和选择的id。如果不加[ ],结果如标题所述,在'sr_list3.html'页面只显示最后一条记录,并且url为b以及选择的id。百度了下%5B%5D的意思就是[ ] 。
请问我这个是什么原因,还望不吝赐教,有帮助必采纳!!
:value="item.id"
你的name每次循环都是b,他每次都把一个元素给覆盖掉了,尝试把name变成 b+index ,如下
<input name="{{ 'b'+tem.id}}"/>
1.打印一下obj返回的数据 看它是什么样,在页面进行处理
2.如果是选择状态 看下面
<input type="checkbox" checked="checked" name="{{ 'b'+tem.id}}" value="b" >