gpt4 book ai didi

python - 如何遍历从 Python/Tornado 处理程序传递到 Tornado 模板的字典?

转载 作者:太空狗 更新时间:2023-10-30 01:54:59 26 4
gpt4 key购买 nike

如何遍历从 Python/Tornado 处理程序传递到 Tornado 模板的字典?

我试过

    <div id="statistics-table">
{% for key, value in statistics %}
{{key}} : {{value['number']}}
{% end %}
</div>

但它不起作用,统计数据是字典

statistics = { 1 : {'number' : 2},  2 : {'number' : 8}}

最佳答案

>>> from tornado import template
>>> t = template.Template('''
... <div id="statistics-table">
... {% for key, value in statistics.items() %}
... {{key}} : {{value['number']}}
... {% end %}
... </div>
... ''')
>>> statistics = { 1 : {'number' : 2}, 2 : {'number' : 8}}
>>> print(t.generate(statistics=statistics))

<div id="statistics-table">

1 : 2

2 : 8

</div>

备选方案:

<div id="statistics-table">
{% for key in statistics %}
{{key}} : {{statistics[key]['number']}}
{% end %}
</div>

关于python - 如何遍历从 Python/Tornado 处理程序传递到 Tornado 模板的字典?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17148732/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com