猿问

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

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


我试过像


    <div id="statistics-table">

            {% for key, value in statistics %}

            {{key}} : {{value['number']}}

            {% end %}

        </div>

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


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


蝴蝶刀刀
浏览 225回答 2
2回答

守候你守候我

>>> from tornado import template>>> t = template.Template('''... <div id="statistics-table">...&nbsp; &nbsp; &nbsp;{% for key, value in statistics.items() %}...&nbsp; &nbsp; &nbsp;{{key}} : {{value['number']}}...&nbsp; &nbsp; &nbsp;{% end %}... </div>... ''')>>> statistics = { 1 : {'number' : 2},&nbsp; 2 : {'number' : 8}}>>> print(t.generate(statistics=statistics))<div id="statistics-table">&nbsp; &nbsp; 1 : 2&nbsp; &nbsp; 2 : 8</div>选择:<div id="statistics-table">&nbsp; &nbsp; {% for key in statistics %}&nbsp; &nbsp; {{key}} : {{statistics[key]['number']}}&nbsp; &nbsp; {% end %}</div>
随时随地看视频慕课网APP

相关分类

Python
我要回答