首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >循环回溯的Django模板,最后两次迭代

循环回溯的Django模板,最后两次迭代
EN

Stack Overflow用户
提问于 2017-08-22 00:23:22
回答 1查看 129关注 0票数 0

我使用Django 1.10和Python3.5。

我得到了一些代码,这些代码由一个按字母顺序返回3迭代值的循环组成。

例如,For循环返回:Chronological, Combination, Functional

但是,我需要for循环来返回:Chronological, Functional, Combination (最后两个迭代/值是相反的)。我被迫保留for循环。

使用for循环可以做到这一点吗?

我尝试过将{% if forloop.last %}{% if forloop.first%}相结合,并使用{% if forloop.counter == 2 %}...set value to Functional...{% endif %}{% if forloop.counter == 3 %}...set value to Combination...{% endif %}设置值,但我无法实现我想要的结果。

这是我的代码:

代码语言:javascript
复制
{% for resume_format_image in resume_format_images %}
    <div class="col-md-4">
        {% with "resumes/resume_format_description_"|add:resume_format_image.style.lower|add:".html" as template_name %}
            {% include template_name %}
        {% endwith %}
    </div>
{% endfor %}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-22 00:52:59

我刚想明白了:

代码语言:javascript
复制
{% for resume_format_image in resume_format_images %}
    <div class="col-md-4">
        {% if forloop.first%}
            {% include "resumes/resume_format_description_chronological.html" %}
        {% elif forloop.last%}
            {% include "resumes/resume_format_description_combination.html" %}
        {% else %}
            {% include "resumes/resume_format_description_functional.html" %}
        {% endif %}
    </div>
{% endfor %}

我希望这能帮上忙。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45807216

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档