如何在 Jinja2中对列表进行排序?

我正在努力做到这一点:

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

但这是不对的... 文件是模糊的... 你怎么做到这一点,在 Jinja2?

102377 次浏览

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) %}