首页 > 解决方案 > 如何在表中插入过滤列表并为每列添加额外的命名行

问题描述

我有一个过滤值列表SQL

views.py中

name_list=list(AB.objects.filter(name__in=xyz).values_list('name', 'surname', 'sector','industry', 'country','city'))

template.html中:

<ul><li>{% for name, surname,sector,industry,country,city in name_list %} {{ name }} </li></ul>
<ul><li>{% for name, surname,sector,industry,country,city in name_list %} {{surname}} </li></ul>
<ul><li>{% for name, surname,sector,industry,country,city in name_list %} {{sector}} </li></ul>
<ul><li>{% for name, surname,sector,industry,country,city in name_list %} {{industry}} </li></ul>
<ul><li>{% for name, surname,sector,industry,country,city in name_list %} {{country}} </li></ul>
<ul><li>{% for name, surname,sector,industry,country,city in name_list %} {{city}} </li></ul>

它的输出是:

Alex   Klein       Machinery    Aerospace  USA      Kansas
Lia    Michelle    Healthcare   Drugs      Ireland  Dublin

我想为每一列添加名称,例如姓名、姓氏、部门、行业、国家、城市,并将它们插入表中。我做了ulli 因为我不知道如何将它们插入表格

所需的输出是以下方式的表格:

Name   Surname     Sector       Industry   Country  City

Alex   Klein       Machinery    Aerospace  USA      Kansas
Lia    Michelle    Healthcare   Drugs      Ireland  Dublin

感谢您的帮助。

标签: htmldjango

解决方案


每次你写这个词时,for你都会开始一个循环。尝试这个:

{% for name, surname,sector,industry,country,city in name_list %}
<ul><li> {{name}} </li></ul>
<ul><li> {{surname}} </li></ul>
<ul><li> {{sector}} </li></ul>
<ul><li> {{industry}} </li></ul>
<ul><li> {{country}} </li></ul>
<ul><li> {{city}} </li></ul>
{% endfor %}

更新:

按照评论中的要求将其呈现为表格:

<table>
<tbody>
{% for name, surname,sector,industry,country,city in name_list %}
<tr>
<td>{{name}}</td>
<td>{{surname}}</td>
<td>{{sector}}</td>
<td>{{industry}}</td>
<td>{{country}}</td>
<td>{{city}}</td>
</tr>
{% endfor %}
</tbody>
</table>

推荐阅读