简体   繁体   中英

How do I filter a collection in a jinja2 template ?

I have a collection of namedtuples which looks like this :

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

I pass this collection to one of my templates. I'd like to filter that collection based on state. Tried the following to no avail

  {% 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.

使用equalto测试:

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

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