我有一个来自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
我想添加名字到每一列,如姓名,姓氏,部门,行业,国家,城市,并插入他们在表。 我做了ul
和li
,因为我不知道如何在表中插入它们
所需输出按以下方式表:
Name Surname Sector Industry Country City
Alex Klein Machinery Aerospace USA Kansas
Lia Michelle Healthcare Drugs Ireland Dublin
非常感谢你的帮助。
每次为编写字时,都是在启动一个循环。 请尝试以下操作:
{% 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>