简体   繁体   English

Django - 在 HTML 表格中打印列表列表字典

[英]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 ?这是我得到的最终结果,但它的重复键数据以及如何迭代其他键以将下一个键打印为表数据?

HTML 中的表格

Okay, I don't fully understand your question but here are some tips to refactor that.好的,我不完全理解你的问题,但这里有一些重构的技巧。

  1. You should be specific with template syntax.您应该具体使用模板语法。 If it is a dictionary, try using dict.items如果是字典,请尝试使用 dict.items
{% for key, value in dict.items %} 
    {{ key }}, {{ value }} 
{% endfor %}
  1. You could use custom filters.您可以使用自定义过滤器。 See https://docs.djangoproject.com/en/4.0/howto/custom-template-tags/ for more information.有关更多信息,请参阅https://docs.djangoproject.com/en/4.0/howto/custom-template-tags/ By writing your own filters you can use your dictionary on your intention using pure python (either doing something like dict['hostname'][0] for accessing first element from list of a hostname key in a dict).通过编写自己的过滤器,您可以使用纯 python 来使用您的字典(或者执行类似 dict['hostname'][0] 的操作来访问字典中主机名键列表中的第一个元素)。 I wrote it roughly but hope you got the idea, if any questions don't hesitate to ask.我写得很粗略,但希望你能明白,如果有任何问题,请不要犹豫。 Additionally, you could filter items in a list which is a value in your dictionary.此外,您可以过滤列表中的项目,该列表是字典中的值。 For example:例如:
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.

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