Python 只需在for循环中打印一次语句-Django

Python 只需在for循环中打印一次语句-Django,python,django,django-templates,Python,Django,Django Templates,因此,在我的模板中,我只需要在for循环中打印一条语句,即使它迭代了很多次。我的代码是: {% for interview in scheduledinterviews %} {% if interview.slot.start.date == today.date %} <div class="row"> <div class="col-xs-12 profile-dash"> <h4>Today's Interview

因此,在我的模板中,我只需要在for循环中打印一条语句,即使它迭代了很多次。我的代码是:

{% for interview in scheduledinterviews %}
    {% if interview.slot.start.date == today.date %}
<div class="row">

    <div class="col-xs-12 profile-dash">
          <h4>Today's Interviews</h4>
    </div>
</div>
{%用于scheduledinterviews%中的访谈]
{%if-interview.slot.start.date==today.date%}
今天的采访
我只需要打印一次《今日访谈》,因为它在循环中满足if条件。我该怎么办?

您可以使用标签

{%ifchanged interview.slot.start.date%}今天的访谈{%endifchanged%}
您可以使用标签

{%ifchanged interview.slot.start.date%}今天的访谈{%endifchanged%}

很抱歉让您失望,django模板中没有break标记。对我来说,最简单的方法是在呈现数据之前在各自的视图中更好地准备上下文数据。

很抱歉让您失望,但django模板中没有中断标记。对我来说,最简单的方法是在呈现数据之前,在相应的视图中更好地准备上下文数据。

{%ifchanged%}在单循环下工作,但我在两个循环内尝试,它不工作。例如-{%fori在j%}{%fork在j%}{%ifchanged%}//{%endifchanged%}{%endfor%}{%endfor%}{%endfor%}但在这种情况下它不起作用{%ifchanged%}在单循环下起作用,但我在两个循环内尝试,它不起作用。例如-{%i在j%}{%k在j%}{%ifchanged%}//{%endifchanged%}{%endfor%}{%endfor%}{%endfor%}但是在这种情况下它不起作用了你想做什么?您是否正在尝试检查scheduledinterviews是否为空??如果您只想运行for循环一次,就可以使用slice!比如{%if scheduledinterviews}今天的访谈{%endif%}你想做什么?你想检查scheduledinterviews是否为空吗?你可以像{%if scheduledinterviews}今天的访谈{%endif%}如果只想运行for循环一次,可以使用slice!比如{scheduledinterviews中的%interview}slice:“1”%}
{% ifchanged interview.slot.start.date %}<h4>Today's Interviews</h4>{% endifchanged %}