我想根据变量过滤for loop
一个过滤器。这就是我正在做的事情:groupby
loop
{% for group in list_of_dicts | groupby('attribute') -%}
{% if loop.index < 9 %}
...
{% endif %}
{% endfor %}
它按我的预期工作。在文档中有这样的语法:
{% for user in users if not user.hidden %}
<li>{{ user.username|e }}</li>
{% endfor %}
循环过滤器时如何使用上述语法?我的意思是像下面这样,它提出了一个UndefinedError
:
{% for group in list_of_dicts | groupby('attribute') if loop.index < 9 -%}
...
{% endfor %}
UndefinedError: 'loop' is undefined. the filter section of a loop as well as the else block don't have access to the special 'loop' variable of the current loop. Because there is no parent loop it's undefined. Happened in loop on line 18 in 'page.html'