【发布时间】:2021-12-28 15:22:23
【问题描述】:
我有一个嵌套的 python 字典。我想在 html 表格中显示它。我将字典作为上下文变量传递以在 html 中访问它。 但是,我无法让它以我想要的方式显示。我想要显示它的方式是将键作为列,然后将值作为行的数据。 我已经成功了一半:
字典:
dict = {1: {'name': 'John', 'age': '27', 'height': '160'},
2: {'name': 'Marie', 'age': '22', 'height': '170'}}
HTML:
<table class="u-full-width" id="table2">
<thead >
<tr>
</tr>
</thead>
<tbody>
{% for key,value in dict_items.items %}
<tr>
<th scope="row" style="text-align:center">{{ key }}</th>
<td style="text-align:center">{{ value }}</td>
</tr>
{% endfor %}
</tbody>
</table>
当前结果
【问题讨论】:
-
你是怎么走到一半的?你的代码在哪里?
-
在考虑了一段时间之后,听起来您想为此使用
jinja库。第二个示例是 HTML 模板吗?它的用途有点令人困惑。我会说尝试将其转换为 jinja 语法,你应该得到所需的 html 输出。 -
这能回答你的问题吗? Writing nested dictionary to a HTML fle
标签: python html python-3.x html-table