【发布时间】:2015-11-05 17:58:22
【问题描述】:
我有一个 django 模板,我想显示在我的视图中创建的一堆 html 表。我传入一个列表,其中列表中的每个元素都是一个 html 表。我也想给表格加标签,但我无法弄清楚如何正确循环遍历 html 表格列表和标签列表。
在 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)
我的 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>
我知道这是不对的,但我不知道如何正确地做到这一点。我只想让它看起来像这样:
表格标签1
表 1
表格标签2
表 2
【问题讨论】:
-
你的 views.py 有点难以理解。你说“我正在传递一个列表,其中列表中的每个元素都是一个 html 表。”但我看不出你在哪里做这个。我假设我只是一个 dingus 并且想念它。如果有这个列表,我下面的回答应该会对你有所帮助。
标签: python django templates django-templates