我编写了一个嵌套循环,并对来自外循环和内循环的数据进行值比较。下面是我的模板代码:-
<tbody>
{% for col in filter2.qs %}
<tr>
{% for mso in filter1.qs %}
{{ col.box_id }} vs {{ mso.box_id }}
<br>
{% if mso.box_id == forloop.parentloop.col.box_id %}
<td>{{ mso.mso_id }}</td>
<td>{{ col.box_id }}</td>
<td>{{ col.channel_id }}</td>
{% endif %}
{% endfor %}
</tr>
{% empty %}
<tr>
<td colspan="5">No data</td>
</tr>
{% endfor %}
</tbody>
问题是:
{{ col.box_id }} vs {{ mso.box_id }}
时,我可以看到值 {{ col.box_id|length }} vs {{ mso.box_id|length }}
时,我看到外循环值的长度为0。 如果下面的条件永远不会运行,则表中不会插入任何数据。 我正在从views.py
获取两个循环的数据def search(request):
user_list1 = VCB_Execution_Details.objects.all()
user_filter1 = ReportFilter_VCB_Execution_Details(request.GET, queryset=user_list1)
user_list2 = VCB_Details.objects.all()
user_filter2 = ReportFilter_VCB_Details(request.GET, queryset=user_list2)
print(user_filter2.qs)
print(type(user_filter1))
return render(request, 'user_list.html', {'filter1':user_filter2,'filter2': user_filter1})
filters.py
class ReportFilter_VCB_Execution_Details(django_filters.FilterSet):
class Meta:
model = VCB_Execution_Details
fields = ['box_id','channel_id']
class ReportFilter_VCB_Details(django_filters.FilterSet):
class Meta:
model = VCB_Details
fields = ['box_id','mso_id']
答案 0 :(得分:0)
所以最后我得到了答案,问题是对象类型为[{'a': 1, 'b': 2}, {'a': 5, 'b': 6}]
与VCB_Execution_Details
。我尝试通过为user_list1和user_list2的queryset中的每个数据打印变量类型来检查str
中的数据类型