简体   繁体   English

Django字典数据转换成html表

[英]django dictionary data into html table

I have following dictionary output data of python in browser 我在浏览器中有以下python的dictionary输出数据

{'pid': 0, 'name': 'System Process'}
{'pid': 41, 'name': 'System123'}
{'pid': 110, 'name': 'svchost.exe'}
{'pid': 280, 'name': 'smss.exe'}
{'pid': 336, 'name': 'WSE.exe'}
{'pid': 424, 'name': 'csrss.exe'} 

but want to create a html / bootstrap table in my template such that the output table will look similar to: 但要在我的template创建html / bootstrap table ,以使输出table看起来类似于:

PID   NAME

0     System Idle Process
41    System123
110   svchost.exe
280   smss.exe
336   WSE.exe
..    ..
..    ...

exactly like this Image 完全像这张图片

my template code: 我的模板代码:

<body>
        {% for obj in mydict_key %}
        <table >
            {{ obj }}
        </table>
        {% endfor %}
</body>

Not completely sure about the template syntax, but it would be something like this: 不能完全确定模板的语法,但是可能会像这样:

 <body> <table> <tr> <th>PID</th> <th>NAME</th> </tr> {% for obj in mydict_key %} <tr> <td>{{ obj.pid }}</td> <td>{{ obj.name }}</td> </tr> {% endfor %} </table> </body> 

If you have a list of dicts: 如果您有字典列表:

list_of_dicts = [
    {'pid': 0, 'name': 'System Process'},
    {'pid': 41, 'name': 'System123'},
    {'pid': 110, 'name': 'svchost.exe'},
    {'pid': 280, 'name': 'smss.exe'},
    {'pid': 336, 'name': 'WSE.exe'},
    {'pid': 424, 'name': 'csrss.exe'} 
]

the in the template you can do: 您可以在模板中执行以下操作:

<body>
  <table>
    <tr>
      <th>PID</th>
      <th>NAME</th>
    </tr>
    {% for dict in list_of_dicts %} 
       <tr>
          {% for value in dict.values %}       
            <td>{{ value }}</td>
          {% endfor %}
      </tr>
    {% endfor %}
  </table>
</body> 

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

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