简体   繁体   English

Jinja2 附加到循环时不打印

[英]Jinja2 prints none when appending to a loop

In my flask application I am creating a list and appending.在我的 flask 应用程序中,我正在创建一个列表并附加。 When I append to the list None prints.当我 append 到列表时None打印。

{% set keyword_list = [] %}
{% for keyword in keywords_data %}
    {{ keyword_list.append(keyword['keywords_id']) }} //None prints for each iteration
{% endfor %}

{{ keyword_list | join(',') }} {{ keyword_list | join(',') }} prints 1,2,3 {{ keyword_list }} prints [1,2,3] {{ keyword_list | join(',') }}打印1,2,3 {{ keyword_list }}打印[1,2,3]

Change it to {{ keyword_list.append(keyword['keywords_id']) or "" }} , which uses the feature of or s that returns the first truthy answer.将其更改为{{ keyword_list.append(keyword['keywords_id']) or "" }} ,它使用返回第一个真实答案的or的特征。 Since strings are always truthy in Jinja, and None is falsy, it returns the empty string, outputting nothing to the end-user.由于字符串在 Jinja 中始终为真,而None为假,因此它返回空字符串,不向最终用户输出任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM