html - 如何在表中插入过滤列表并为每列添加额外的命名行
问题描述
我有一个过滤值列表
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
你都会开始一个循环。尝试这个:
{% 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>
推荐阅读
- x86-16 - 如何打印存储在变量中的整数
- python - 混合龙卷风和 sqlalchemy
- java - 在java中使用opencv录制和保存视频流
- r - ggplot构面图重新排序未按预期工作
- algorithm - 我正在努力为我的家庭作业找到有效的解决方案。冒泡排序还是插入排序?
- php - php - 基于范围键返回数组
- kubernetes - 将 2 个不同的 statefulSet 连接到同一个 PV
- architecture - 在 DDD 的上下文中应该如何处理事务
- ruby-on-rails - 更新具有相同图像的模型时出现 ActiveSupport::MessageVerifier::InvalidSignature 错误
- git - 在 Git 中将分支从主分支移动到另一个分支