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

时间:2015-05-21 08:10:49

标签: 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 Interviews</h4>
    </div>
</div>

我只需打印一次今日面试,因为它满足循环内的if条件。我该怎么办?

2 个答案:

答案 0 :(得分:2)

您可以使用ifchanged标记。

{% ifchanged interview.slot.start.date %}<h4>Today's Interviews</h4>{% endifchanged %}

答案 1 :(得分:0)

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

相关问题