python - 在 Django 模板中隐藏重复迭代

标签 python django django-templates

我正在使用 Django。在我的模板中,我使用 for 循环,但不需要打印已打印的相同日期。所以我的代码是-

{% for applicant in applicants %}

<div class="row">
    <div  class="col-xs-12">
        <div class="col-xs-12 col-sm-12 col-md-2">
            <div  class="col-xs-12 application_slot_list">
                <h5>{{ applicant.created_on|date }}</h5>
            </div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-10 candidate_list_application">
            <div class="row">
                <div class="col-xs-12 list-title">
                    <h4>{{ applicant.employee }}<small class="pull-right"><button class="btn btn-link">Download CV</button></small></h4>
                </div>

所以在这里我尝试打印两件事,员工和日期,但我不想打印重复的日期。那么有没有什么办法可以做到这一点。我只想在模板中而不是在 View 中执行此操作。

最佳答案

使用{% ifchanged %}标签:

Check if a value has changed from the last iteration of a loop.

{% for applicant in applicants %}
    <div class="row">
        <div  class="col-xs-12">
            {% ifchanged %}
                <div class="col-xs-12 col-sm-12 col-md-2">
                    <div  class="col-xs-12 application_slot_list">
                        <h5>{{ applicant.created_on|date }}</h5>
                    </div>
                </div>
            {% endifchanged %}
            <div class="col-xs-12 col-sm-12 col-md-10 candidate_list_application">
                <div class="row">
                    <div class="col-xs-12 list-title">
                        <h4>{{ applicant.employee }}<small class="pull-right"><button class="btn btn-link">Download CV</button></small></h4>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endfor %}

关于python - 在 Django 模板中隐藏重复迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30327915/

相关文章:

python - Django:更多 pythonic __unicode__

python - CREATE EXTENSION postgis 后类型几何不存在

python - 删除空行 - openpyxl

django - 从 ModelForm 保存数据

javascript - 服务器端或 Javascript 计算?

python - 在 python 中使用多重处理折叠数组突变

python - 现有模型的 Django-cms 插件

django - 如何在没有迭代的情况下为查询集从多对多中删除对象?

django:从包含的模板中访问 request.get_full_path

html - Django 2.0 : back button redirecting to different pages based on where you came from