[英]Django - Print Dictionary of List of Lists in a Table in HTML
I have a Dictionary of List of Lists.我有一个列表列表字典。 I want to print this as a structured way preferably a table.
我想将其打印为结构化方式,最好是表格。
For each hostname, i want its specified rows in the table with three columns (Address Interface State ID ) with apprpriate entries printed out.对于每个主机名,我希望它在表中的指定行包含三列(地址接口状态 ID),并打印出适当的条目。 It seems a very complicated nested loop if possible.
如果可能的话,这似乎是一个非常复杂的嵌套循环。
Here is the dictionary ospfdictkey being passed to the template这是传递给模板的字典 ospfdictkey
{'hostname': [['R1-ISP'], ['R2-ISP']], 'ospfneighboraddress': [['192.168.5.2', '192.168.5.13', '192.168.5.25'], ['192.168.5.1', '192.168.5.6', '192.168.5.32']], 'ospfneighborinterface': [['ae1.0', 'ae4.0', 'ae7.0'], ['ae1.0', 'ae2.0', 'ae9.0']], 'ospfneighborstate': [['Full', 'Full', 'Full'], ['Full', 'Full', 'Full']], 'ospfneighborID': [['172.0.0.2', '172.0.0.4', '172.0.0.5'], ['172.0.0.1', '172.0.0.3', '172.0.0.6']]}
HTML Code
{% for ip in listip %}
{% for ospfadd in ospfdictkey.ospfneighboraddress %}
{% for a in ospfadd %}
<tr>
<td>{{ a }}</td>
</tr>
{% endfor %}
{% endfor %}
{% endfor %}
Here is the end result im getting but its repeating keys data and how do i iterate over other keys to print next as table data ?这是我得到的最终结果,但它的重复键数据以及如何迭代其他键以将下一个键打印为表数据?
Okay, I don't fully understand your question but here are some tips to refactor that.好的,我不完全理解你的问题,但这里有一些重构的技巧。
{% for key, value in dict.items %}
{{ key }}, {{ value }}
{% endfor %}
if len(dictionary['hostname'] > 2: # if you want to get only those values which length is greater than 2
# return something
PS. PS。 try to write readable code in your editor and in here asking questions like so:
尝试在您的编辑器中编写可读代码,并在这里提出如下问题:
{
'hostname': [
['R1-ISP'],
['R2-ISP'],
],
'ospfneighboraddress': [
['192.168.5.2', '192.168.5.13', '192.168.5.25'],
['192.168.5.1', '192.168.5.6', '192.168.5.32'],
],
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.