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

时间:2021-01-09 20:45:13

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?

但那不对......文档含糊不清......你怎么在Jinja2做到这一点?

3 个解决方案

#1


114  

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

从2.6版本开始,Jinja2的内置排序过滤器允许您指定要排序的属性:

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

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

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

#2


11  

Usually we sort the list before giving it to Jinja2. There's no way to specify a key in Jinja's sort filter.

通常我们会在将列表提供给Jinja2之前对其进行排序。在Jinja的排序过滤器中无法指定密钥。

However, you can always try {% for movie in movie_list|sort %}. That's the syntax. You don't get to provide any sort of key information for the sorting.

但是,您可以随时尝试{%for movie in movie_list | sort%}。这就是语法。您无法为排序提供任何类型的关键信息。

You can also try and write a custom filter for this. Seems silly when you can sort before giving the data to Jinja2.

您也可以尝试为此编写自定义过滤器。在将数据提供给Jinja2之前,您可以进行排序,这似乎很愚蠢。

If movie_list is a list of objects, then you can define the various comparison methods (__lt__, __gt__, etc.) for the class of those objects.

如果movie_list是一个对象列表,那么您可以为这些对象的类定义各种比较方法(__lt __,__ gt__等)。

If movie_list is a list of tuples or lists, the rating must be first. Or you'll have to do the sorting outside Jinja2.

如果movie_list是元组或列表的列表,则必须先评级。或者你必须在Jinja2之外进行排序。

If movie_list is a list of dictionaries, then you can use dictsort, which does accept a key specification for the sorting. Read this: http://jinja.pocoo.org/2/documentation/templates#dictsort for an example.

如果movie_list是字典列表,那么您可以使用dictsort,它接受排序的密钥规范。请阅读:http://jinja.pocoo.org/2/documentation/templates#dictsort作为示例。

#3


9  

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

#1


114  

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

从2.6版本开始,Jinja2的内置排序过滤器允许您指定要排序的属性:

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

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

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

#2


11  

Usually we sort the list before giving it to Jinja2. There's no way to specify a key in Jinja's sort filter.

通常我们会在将列表提供给Jinja2之前对其进行排序。在Jinja的排序过滤器中无法指定密钥。

However, you can always try {% for movie in movie_list|sort %}. That's the syntax. You don't get to provide any sort of key information for the sorting.

但是,您可以随时尝试{%for movie in movie_list | sort%}。这就是语法。您无法为排序提供任何类型的关键信息。

You can also try and write a custom filter for this. Seems silly when you can sort before giving the data to Jinja2.

您也可以尝试为此编写自定义过滤器。在将数据提供给Jinja2之前,您可以进行排序,这似乎很愚蠢。

If movie_list is a list of objects, then you can define the various comparison methods (__lt__, __gt__, etc.) for the class of those objects.

如果movie_list是一个对象列表,那么您可以为这些对象的类定义各种比较方法(__lt __,__ gt__等)。

If movie_list is a list of tuples or lists, the rating must be first. Or you'll have to do the sorting outside Jinja2.

如果movie_list是元组或列表的列表,则必须先评级。或者你必须在Jinja2之外进行排序。

If movie_list is a list of dictionaries, then you can use dictsort, which does accept a key specification for the sorting. Read this: http://jinja.pocoo.org/2/documentation/templates#dictsort for an example.

如果movie_list是字典列表,那么您可以使用dictsort,它接受排序的密钥规范。请阅读:http://jinja.pocoo.org/2/documentation/templates#dictsort作为示例。

#3


9  

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