如何在jinja2模板中过滤集合?

时间:2016-01-24 10:32:41

标签: python python-2.7 filter jinja2

我有一个名为thetuples的集合,如下所示:

[('mountpoint=X',state='UP'),(mountpoint='Y',state='DOWN'),(mountpoint='Z',state='DOWN'...)]

我将此集合传递给我的一个模板。我想根据状态过滤该集合。尝试以下无济于事

  {% for state in states|selectattr('state','down') %}
TemplateRuntimeError: no test named 'down'

  {% for state in states|selectattr(state='down') %}
FilterArgumentError: Missing parameter for attribute name

  {% for state in states|select(state='down') %}
No error, but doesn't filter at all.

1 个答案:

答案 0 :(得分:2)

使用equalto测试:

{% for state in states|selectattr('state', 'equalto', 'down') %}