简体   繁体   中英

Jinja2 reverse a list

Hey i want to reverse the output of the following. Can you help me?

{% set page 7 %}
{% for i in range(1,6) %}
    {% set back_link = page - i %}
    {{back_link}}
 {% endfor %}

Output: 6 5 4 3 2

Wanted: 2 3 4 5 6

You can reverse a list in Jinja2 using the reverse filter

{% for x in mylist|reverse %}
    {{x}}
{% endfor %}

This is compatible with range so you could use:

{% for i in range(1, 10)|reverse %}
    {{i}}
{% endfor %}

如果要向后递增,则可以使用range(6,1,-1),第三个参数是要使用的增量( 在此处记录 )。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM