外循环值与内循环值不匹配:Django模板

时间:2018-06-21 13:35:02

标签: django django-templates

我编写了一个嵌套循环,并对来自外循环和内循环的数据进行值比较。下面是我的模板代码:-

<tbody>
         {% for col in filter2.qs %}
           <tr>
             {% for mso in filter1.qs %}
                {{ col.box_id }}&nbsp;vs&nbsp;{{ 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>

问题是:

  1. 当我打印{{ col.box_id }}&nbsp;vs&nbsp;{{ mso.box_id }}时,我可以看到值enter image description here
  2. 当我打印{{ col.box_id|length }}&nbsp;vs&nbsp;{{ mso.box_id|length }}时,我看到外循环值的长度为0。enter image description here

如果下面的条件永远不会运行,则表中不会插入任何数据。 我正在从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']

1 个答案:

答案 0 :(得分:0)

所以最后我得到了答案,问题是对象类型为[{'a': 1, 'b': 2}, {'a': 5, 'b': 6}] VCB_Execution_Details。我尝试通过为user_list1和user_list2的queryset中的每个数据打印变量类型来检查str中的数据类型