在 html table 中显示嵌套的 python 字典
Displaying nested python dictionary in html table
我有一个嵌套的 python 字典。我想在 html table 中显示它。我将字典作为上下文变量传递以在 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>
当前结果
如果那是 Django,请更改此行
<td style="text-align:center">{{ value }}</td>
至
<td style="text-align:center">{{ value.name }}</td>
<td style="text-align:center">{{ value.age }}</td>
<td style="text-align:center">{{ value.height }}</td>
如果对您有帮助,请不要忘记采纳为正确答案。
我有一个嵌套的 python 字典。我想在 html table 中显示它。我将字典作为上下文变量传递以在 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>
当前结果
如果那是 Django,请更改此行
<td style="text-align:center">{{ value }}</td>
至
<td style="text-align:center">{{ value.name }}</td>
<td style="text-align:center">{{ value.age }}</td>
<td style="text-align:center">{{ value.height }}</td>
如果对您有帮助,请不要忘记采纳为正确答案。