简体   繁体   中英

Displaying nested python dictionary in html table

I have a nested python dictionary. I want to display this in a html table. I am passing the dictionary as a context variable to access it in 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:

<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

 <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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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