开发者

How do you sort a list in Jinja2?

开发者 https://www.devze.com 2022-12-14 16:31 出处:网络
I am trying 开发者_高级运维to do this: {% for movie in movie_list | sort(movie.rating) %} But that\'s not right...the documentation is vague...how do you do this in Jinja2?As of version 2.6, Jinja

I am trying 开发者_高级运维to do this:

 {% for movie in movie_list | sort(movie.rating) %}

But that's not right...the documentation is vague...how do you do this in Jinja2?


As of version 2.6, Jinja2's built-in sort filter allows you to specify an attribute to sort by:

{% for movie in movie_list|sort(attribute='rating') %}

See http://jinja.pocoo.org/docs/templates/#sort


If you want to sort in ascending order

{% for movie in movie_list|sort(attribute='rating') %}

If you want to sort in descending order

{% for movie in movie_list|sort(attribute='rating', reverse = True) %}
0

精彩评论

暂无评论...
验证码 换一张
取 消