【问题标题】:Displaying nested python dictionary in html table [closed]在html表中显示嵌套的python字典[关闭]
【发布时间】: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


【解决方案1】:

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

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

【讨论】:

  • 感谢这个完美的工作
猜你喜欢
  • 1970-01-01
  • 2019-09-12
  • 1970-01-01
  • 1970-01-01
  • 2013-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多