How to iterate through dictionary passed from Python/Tornado handler to Tornado's template ?
I tried like
<div id="statistics-table">
{% for key, value in statistics %}
{{key}} : {{value['number']}}
{% end %}
</div>
Run Code Online (Sandbox Code Playgroud)
but it doesn't work, where statistics is dictionary
statistics = { 1 : {'number' : 2}, 2 : {'number' : 8}}
Run Code Online (Sandbox Code Playgroud)
fal*_*tru 10
>>> 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>
Run Code Online (Sandbox Code Playgroud)
替代方案:
<div id="statistics-table">
{% for key in statistics %}
{{key}} : {{statistics[key]['number']}}
{% end %}
</div>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3104 次 |
| 最近记录: |