获取 jinja2 模板中列表的长度

发布于 2024-08-05 10:52:31 字数 229 浏览 5 评论 0原文

如何获取 jinja2 模板中列表中的元素数量?

例如,在 Python 中:

print(template.render(products=[???]))

以及在 jinja2 中

<span>You have {{what goes here?}} products</span>

How do I get the number of elements in a list in jinja2 template?

For example, in Python:

print(template.render(products=[???]))

and in jinja2

<span>You have {{what goes here?}} products</span>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一念一轮回 2024-08-12 10:52:31
<span>You have {{products|length}} products</span>

您还可以在表达式中使用此语法,例如

{% if products|length > 1 %}

此处记录了 jinja2 的内置过滤器;具体来说,正如您已经发现的, length (及其同义词 count)被记录为:

返回序列或映射的项目数。

因此,正如您所发现的,模板中的 {{products|count}} (或等效的 {{products|length}})将给出“数量产品”(“列表长度”)

<span>You have {{products|length}} products</span>

You can also use this syntax in expressions like

{% if products|length > 1 %}

jinja2's builtin filters are documented here; and specifically, as you've already found, length (and its synonym count) is documented to:

Return the number of items of a sequence or mapping.

So, again as you've found, {{products|count}} (or equivalently {{products|length}}) in your template will give the "number of products" ("length of list")

你的他你的她 2024-08-12 10:52:31

亚历克斯的评论看起来不错,但我仍然对使用范围感到困惑。
在使用范围内的长度处理条件时,以下内容对我有用。

{% for i in range(0,(nums['list_users_response']['list_users_result']['users'])| length) %}
<li>    {{ nums['list_users_response']['list_users_result']['users'][i]['user_name'] }} </li>
{% endfor %}

Alex' comment looks good but I was still confused with using range.
The following worked for me while working on a for condition using length within range.

{% for i in range(0,(nums['list_users_response']['list_users_result']['users'])| length) %}
<li>    {{ nums['list_users_response']['list_users_result']['users'][i]['user_name'] }} </li>
{% endfor %}
将军与妓 2024-08-12 10:52:31

我遇到过长度为 None 的问题,这会导致内部服务器错误: TypeError: object of type 'NoneType' has no len()

我的解决方法是如果对象为 None 则显示 0 并计算其他类型的长度,例如列表就我而言:

{{'0' if linked_contacts == None else linked_contacts|length}}

I've experienced a problem with length of None, which leads to Internal Server Error: TypeError: object of type 'NoneType' has no len()

My workaround is just displaying 0 if object is None and calculate length of other types, like list in my case:

{{'0' if linked_contacts == None else linked_contacts|length}}
婴鹅 2024-08-12 10:52:31

如果使用带有数组的 for 循环,则可以使用以下内容

{% for i in range(array|length) %}
          array[i]
{% endfor %}

If for loop with array is used then you can use the following

{% for i in range(array|length) %}
          array[i]
{% endfor %}
倒数 2024-08-12 10:52:31

尝试一下:

<span>You have {% for n in products %}{% if loop.index == 1 %}{{ loop.length }}{% endif %}{% endfor %} products</span>

Try it:

<span>You have {% for n in products %}{% if loop.index == 1 %}{{ loop.length }}{% endif %}{% endfor %} products</span>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文