繁体   English   中英

在 html 表中显示嵌套的 python 字典

[英]Displaying nested python dictionary in html table

我有一个嵌套的 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>

当前结果

在此处输入图像描述

如果那是 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>

如果这对您有帮助,请不要忘记接受这个作为正确答案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM