简体   繁体   English

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

[英]Displaying nested python dictionary in html table

I have a nested python dictionary.我有一个嵌套的 python 字典。 I want to display this in a html table.我想在 html 表中显示它。 I am passing the dictionary as a context variable to access it in html.我将字典作为上下文变量传递以在 html 中访问它。 However, I cannot get it to display the way I want.但是,我无法让它以我想要的方式显示。 The way I want to display it is to have the keys as columns and then the values as the data for the rows.我想要显示它的方式是将键作为列,然后将值作为行的数据。 I have kind of got half way there:我已经成功了一半:

Dictionary:字典:

dict = {1: {'name': 'John', 'age': '27', 'height': '160'},
          2: {'name': 'Marie', 'age': '22', 'height': '170'}}

Html: 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>

current result当前结果

在此处输入图像描述

if that is Django change this line如果那是 Django 更改此行

 <td style="text-align:center">{{ value }}</td>

to

 <td style="text-align:center">{{ value.name }}</td>
 <td style="text-align:center">{{ value.age }}</td>
 <td style="text-align:center">{{ value.height }}</td>

if that helped you, please don't forget to accept this as the correct answer.如果这对您有帮助,请不要忘记接受这个作为正确答案。

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

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