Jinja 2中列表的元素

时间:2022-10-06 07:42:04

I have list in Jinja2 that contain dicts in itself. Something like

我在Jinja2中列出了包含dicts的列表。就像是

items = [{'name':'name1', 'points':5}, {'name':'name2', 'points':7}, 
 {'name':'name3', 'points':2}, {'name':'name4', 'points':11}]

What I need is to get sum of all points and to print it somewhere later.

我需要的是获得所有点的总和并在以后的某个地方打印它。

Currently what I got is:

目前我得到的是:

{% set points = 0 -%}
{% for single_item in items -%}
    {% set points = points + single_item["points"] -%}
    {{points}}
{% endfor %}
{{ points }}

Result is: 5 12 14 25 0

结果是:5 12 14 25 0

Is there any way that I can get that points outside of loop has value 25 (last value from the loop)?

有什么方法可以得到循环外的点有值25(循环的最后一个值)?

3 个解决方案

#1


9  

Jinja2 includes a sum filter which will do this for you:

Jinja2包含一个和过滤器,它将为您执行此操作:

{{ items | sum(attribute='points') }}

See documentation here: http://jinja.pocoo.org/docs/dev/templates/#sum

请参阅此处的文档:http://jinja.pocoo.org/docs/dev/templates/#sum

#2


4  

I've managed to make it work, although solution is not elegant, but it is working:

我已经设法使它工作,虽然解决方案不优雅,但它正在工作:

{% set points = [0] -%}
{% for single_item in items -%}
    {% if points.append(points.pop()+ single_item["points"]) -%}{% endif %}
{% endfor %}
{{ points }}

points will be array with just one element that has sum.

points将是只有一个具有sum的元素的数组。

It can be also done with included do extension, and that would replace {% if %} line.

它也可以使用包含的do扩展来完成,这将取代{%if%}行。

#3


2  

That sort of logic should usually go in the controller, not the template (separating logic from presentation). Preprocess your data accordingly, and pass items as well as total to the template:

这种逻辑通常应该放在控制器中,而不是模板中(将逻辑与表示分开)。相应地预处理您的数据,并将项目和总数传递给模板:

from jinja2 import Template

template = Template(open('index.html').read())

items = [{'name': 'name1', 'points': 5},
         {'name': 'name2', 'points': 7},
         {'name': 'name3', 'points': 2},
         {'name': 'name4', 'points': 11}]

total = sum([i['points'] for i in items])

print template.render(items=items, total=total)

index.html:

index.html的:

<table>

{% for item in items %}
  <tr>
    <td>{{ item.name }}</td>
    <td>{{ item.points }}</td>
  </tr>
{% endfor %}

</table>

<strong>Total:</strong>{{ total }}

For details on the expression sum([i['points'] for i in items]) see list comprehensions.

有关表达式sum的详细信息(项目中i的[i ['points']),请参阅列表推导。

#1


9  

Jinja2 includes a sum filter which will do this for you:

Jinja2包含一个和过滤器,它将为您执行此操作:

{{ items | sum(attribute='points') }}

See documentation here: http://jinja.pocoo.org/docs/dev/templates/#sum

请参阅此处的文档:http://jinja.pocoo.org/docs/dev/templates/#sum

#2


4  

I've managed to make it work, although solution is not elegant, but it is working:

我已经设法使它工作,虽然解决方案不优雅,但它正在工作:

{% set points = [0] -%}
{% for single_item in items -%}
    {% if points.append(points.pop()+ single_item["points"]) -%}{% endif %}
{% endfor %}
{{ points }}

points will be array with just one element that has sum.

points将是只有一个具有sum的元素的数组。

It can be also done with included do extension, and that would replace {% if %} line.

它也可以使用包含的do扩展来完成,这将取代{%if%}行。

#3


2  

That sort of logic should usually go in the controller, not the template (separating logic from presentation). Preprocess your data accordingly, and pass items as well as total to the template:

这种逻辑通常应该放在控制器中,而不是模板中(将逻辑与表示分开)。相应地预处理您的数据,并将项目和总数传递给模板:

from jinja2 import Template

template = Template(open('index.html').read())

items = [{'name': 'name1', 'points': 5},
         {'name': 'name2', 'points': 7},
         {'name': 'name3', 'points': 2},
         {'name': 'name4', 'points': 11}]

total = sum([i['points'] for i in items])

print template.render(items=items, total=total)

index.html:

index.html的:

<table>

{% for item in items %}
  <tr>
    <td>{{ item.name }}</td>
    <td>{{ item.points }}</td>
  </tr>
{% endfor %}

</table>

<strong>Total:</strong>{{ total }}

For details on the expression sum([i['points'] for i in items]) see list comprehensions.

有关表达式sum的详细信息(项目中i的[i ['points']),请参阅列表推导。