Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用django模板中的if条件消除重复数据?_Python_Html_Django - Fatal编程技术网

Python 如何使用django模板中的if条件消除重复数据?

Python 如何使用django模板中的if条件消除重复数据?,python,html,django,Python,Html,Django,在模板或html中使用if条件来消除重复数据可以吗 注意:我在视图中已经有了不同的核心,但由于我在1中有两个循环,因此它复制了一些数据 这是我的html {% for core in cores %} {% for behavior in behaviors %} {% if core.Grading_Behavior__Grading_Behavior__Name == behavior.Grading_Behavior__Grading_Behavior__Name

在模板或html中使用if条件来消除重复数据可以吗

注意:我在视图中已经有了不同的核心,但由于我在1中有两个循环,因此它复制了一些数据

这是我的html

{% for core in cores %}
    {% for behavior in behaviors %}
        {% if core.Grading_Behavior__Grading_Behavior__Name == behavior.Grading_Behavior__Grading_Behavior__Name %}
            <tr>
                <td rowspan="2" colspan="4" class="tblcoretitle">{{core.Grading_Behavior__Grading_Behavior__Name}} 1</td>
                {% if core.Grading_Behavior__Grading_Behavior__GroupName == behavior.Grading_Behavior__Grading_Behavior__GroupName  %}
                    <td colspan="4" class="tblcore"> {{behavior.Grading_Behavior__Grading_Behavior__GroupName}}
                    </td>
                {% else %}
                {% endif %}
                <td class="tblcore">1</td>
                <td class="tblcore">2</td>
                <td class="tblcore">3</td>
                <td class="tblcore">4</td>
            </tr>
            <tr>
                {% if core.Grading_Behavior__Grading_Behavior__GroupName == behavior.Grading_Behavior__Grading_Behavior__GroupName  %}
                {% else %}
                    <td colspan="4" class="tblcore">{{behavior.Grading_Behavior__Grading_Behavior__GroupName}}
                    </td>
                {% endif %}
            </tr>
        {% endif %}
    {% endfor %}
{% endfor %}
这是我目前的结果

这是我想要的结果

如果你有更好的解决方案或想法,请分享你的答案

更新

当我在视图中尝试此操作时

cores = StudentsBehaviorGrades.objects.filter(Teacher=teacher) \
        .filter(Students_Enrollment_Records__in=Students.values_list('id')).values(
        'Grading_Behavior__Grading_Behavior__Name', 'Grading_Behavior__Grading_Behavior__GroupName').distinct(
        'Grading_Behavior__Grading_Behavior__Name') \
        .order_by('Grading_Behavior__Grading_Behavior__Name')

    behaviors = StudentsBehaviorGrades.objects.filter(Teacher=teacher) \
        .filter(Students_Enrollment_Records__in=Students.values_list('id')).values(
        'Grading_Behavior__Grading_Behavior__Name', 'Grading_Behavior__Grading_Behavior__GroupName').distinct(
        'Grading_Behavior__Grading_Behavior__GroupName') \
        .order_by('Grading_Behavior__Grading_Behavior__GroupName')

    matches = cores.union(behaviors)
我的html

    {% for match in matches %}
        <tr>
            <td rowspan="2" colspan="4" class="tblcoretitle">{{match.Grading_Behavior__Grading_Behavior__Name}} 1</td>
        </tr>
        <tr>
            <td colspan="4" class="tblcore">{{match.Grading_Behavior__Grading_Behavior__GroupName}}</td>
            <td class="tblcore">1</td>
            <td class="tblcore">2</td>
            <td class="tblcore">3</td>
            <td class="tblcore">4</td>
        </tr>
    {% endfor %}
结果是


既然在同一个对象中使用了两个过滤器,为什么不合并它们以避免重复,并在模板中使用两个for循环呢

import itertools

cores = StudentsBehaviorGrades.objects.filter(Teacher=teacher) \
            .filter(Students_Enrollment_Records__in=Students.values_list('id')).values('Grading_Behavior__Grading_Behavior__Name','Grading_Behavior__Grading_Behavior__GroupName').distinct('Grading_Behavior__Grading_Behavior__Name')\
        .order_by('Grading_Behavior__Grading_Behavior__Name')


behaviors = StudentsBehaviorGrades.objects.filter(Teacher=teacher) \
            .filter(Students_Enrollment_Records__in=Students.values_list('id')).values('Grading_Behavior__Grading_Behavior__Name','Grading_Behavior__Grading_Behavior__GroupName').distinct('Grading_Behavior__Grading_Behavior__GroupName')\
        .order_by('Grading_Behavior__Grading_Behavior__GroupName')

matches = itertools.chain(cores, behaviors)

既然在同一个对象中使用了两个过滤器,为什么不合并它们以避免重复,并在模板中使用两个for循环呢

import itertools

cores = StudentsBehaviorGrades.objects.filter(Teacher=teacher) \
            .filter(Students_Enrollment_Records__in=Students.values_list('id')).values('Grading_Behavior__Grading_Behavior__Name','Grading_Behavior__Grading_Behavior__GroupName').distinct('Grading_Behavior__Grading_Behavior__Name')\
        .order_by('Grading_Behavior__Grading_Behavior__Name')


behaviors = StudentsBehaviorGrades.objects.filter(Teacher=teacher) \
            .filter(Students_Enrollment_Records__in=Students.values_list('id')).values('Grading_Behavior__Grading_Behavior__Name','Grading_Behavior__Grading_Behavior__GroupName').distinct('Grading_Behavior__Grading_Behavior__GroupName')\
        .order_by('Grading_Behavior__Grading_Behavior__GroupName')

matches = itertools.chain(cores, behaviors)

有趣,但我如何在html中调用它?我收到了以下错误断言错误:无法将查询与不同的字段组合起来。mate,我如何在html中调用它?单个for:
{%for match in matches%}{%endfor%}
。您可以删除conditionsmate请检查我的更新问题,以便您查看结果删除,但我如何在html中调用它?我收到此错误断言错误:无法将查询与不同的字段组合。mate,我如何在html中调用它?单个for:
{%for match in matches%}{%endfor%}
。您可以删除conditionsmate请检查我的更新问题,以便查看结果
import itertools

cores = StudentsBehaviorGrades.objects.filter(Teacher=teacher) \
            .filter(Students_Enrollment_Records__in=Students.values_list('id')).values('Grading_Behavior__Grading_Behavior__Name','Grading_Behavior__Grading_Behavior__GroupName').distinct('Grading_Behavior__Grading_Behavior__Name')\
        .order_by('Grading_Behavior__Grading_Behavior__Name')


behaviors = StudentsBehaviorGrades.objects.filter(Teacher=teacher) \
            .filter(Students_Enrollment_Records__in=Students.values_list('id')).values('Grading_Behavior__Grading_Behavior__Name','Grading_Behavior__Grading_Behavior__GroupName').distinct('Grading_Behavior__Grading_Behavior__GroupName')\
        .order_by('Grading_Behavior__Grading_Behavior__GroupName')

matches = itertools.chain(cores, behaviors)