我使用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 %}设置值,但我无法实现我想要的结果。
这是我的代码:
{% 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 %}发布于 2017-08-22 00:52:59
我刚想明白了:
{% 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 %}我希望这能帮上忙。
https://stackoverflow.com/questions/45807216
复制相似问题