简体   繁体   English

在Django模板中循环多个列表

[英]Looping over multiple lists in Django Template

I have a django template that I want to display a bunch of html tables created in my view. 我有一个django模板,我想显示在我的视图中创建的一堆html表。 I'm passing in a list where each element in the list is an html table. 我传入一个列表,其中列表中的每个元素都是一个html表。 I also want to label the tables but I am having trouble figuring out how to loop over the list of html tables and the list of labels correctly. 我也想给表格加上标签,但是我在弄清楚如何正确遍历html表格列表和标签列表时遇到麻烦。

In views.py I create the html tables from simple dataframes: 在views.py中,我从简单的数据帧创建html表:

##task_dict is a dictionary where the values are lists of dataframes.
keys = task_dict.keys()
html_dict = dict.fromkeys(keys,[])

for key in keys:
    for df in task_dict[key]:
        temp_html = df.to_html(classes="table table-hover")
        html_dict[key].append('<br>')
        html_dict[key].append(temp_html)

labels = ['New Plan','New Operation','Current Operation']
html_dict['labels'] = labels

return render(request,'InterfaceApp/SchedulerIngestResults.html',html_dict)

My html template currently looks like this: 我的html模板当前如下所示:

 <div class="panel panel-body">
    {% for df in key1 %}
      {% for l in labels %}
        <div class="panel-heading">{{ l }}
          <div class="table-responsive">
            {{ df | safe }}
          </div>
        </div>
      {% endfor %}
    {% endfor %}
  </div>

I know this isn't right but I can't figure out how to do it correctly. 我知道这是不对的,但是我不知道该怎么做。 All I want is for it to look like this: 我想要的就是它看起来像这样:

Table Label1 表格标签1

Table 1 表格1


Table Label2 表格标签2

Table 2 表2

Since this is kind of difficult troubleshoot without creating a django app from scratch, I'm going to be doing a lot of thinking out loud. 如果不从头开始创建django应用程序,这是一种困难的故障排除,所以我将进行大量的思考。

First, let's look at your template. 首先,让我们看一下您的模板。 You have 你有

 <div class="panel panel-body">
    {% for df in key1 %}
      {% for l in labels %}
        <div class="panel-heading">{{ l }}
          <div class="table-responsive">
            {{ df | safe }}
          </div>
        </div>
      {% endfor %}
    {% endfor %}
</div>

Now, let's set key1 and labels to something simple for this. 现在,让我们将key1labels设置为简单的方法。 How about ... 怎么样 ...

key1 = {'df1' : df1TableHTML, 'df2' : df2TableHTML}
labels = ['label1', 'label2']

Now, let's see how you're for-loops will structure the html. 现在,让我们来看一下for-loops将如何构造html。

<div class="panel panel-body">

  <div class="panel-heading"> label1
    <div class="table-responsive">
      df1TableHTML
    </div>
  </div>

  <div class="panel-heading"> label2
    <div class="table-responsive">
      df1TableHTML
    </div>
  </div>

  <div class="panel-heading"> label1
    <div class="table-responsive">
      df2TableHTML
    </div>
  </div>

  <div class="panel-heading"> label2
    <div class="table-responsive">
      df2TableHTML
    </div>
  </div>

</div>

I'm sure at this point you can see where your problem is. 我确定您现在可以看到问题所在。 Since you are using a nested for-loop, you end up with much more output than you originally wanted. 由于您使用的是嵌套的for循环,因此最终得到的输出比您原来想要的要多得多。 To solve this, you want to iterate over key1 and labels simultaneously. 为了解决这个问题,您想同时遍历key1labels

A pythonic way of doing this is by using the zip() command. 一种执行此操作的Python方法是使用zip()命令。

>>> a = ['a','b','c']
>>> b = ['A','B','C']
>>> for x in zip(a,b):
...     print x
... 
('a', 'A')
('b', 'B')
('c', 'C')

However, Django doesn't have a zip equivlent for a template tag. 但是,Django的模板标签没有zip等效文件。 This means you'll have to do this "zipping" server side in your views.py. 这意味着您必须在views.py中进行“压缩”服务器端。 Do something like below. 做下面的事情。

...
labels = ['New Plan','New Operation','Current Operation']
html_dict['labels_tables'] = zip(label, list_of_tables)

Then in your template, do this. 然后在模板中执行此操作。

<div class="panel panel-body">
  {% for lt in label_table %}
    <div class="panel-heading">{{ lt.0 }}
      <div class="table-responsive">
        {{ lt.1 | safe }}
      </div>
    </div>
  {% endfor %}
</div>

Hopefully this answers your question and sorry for the verbose answer. 希望这能回答您的问题,并为冗长的回答感到抱歉。 Let me know if this works and I'm happy to give you further help if this isn't what you were looking for. 让我知道这是否可行,如果这不是您想要的,很高兴为您提供进一步的帮助。

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

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