How to insert filtered list in table and add additional row of naming for each columns

Viewed 44

I have a list of filtered value from SQL :

in views.py

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

in 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>

Output of it is :

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

I want to add names to each columns such as Name, Surname, Sector, Industry, Country, City and insert them in table. I made ul and li as I di not know how to insert them in table

Desired output is table the following way :

Name   Surname     Sector       Industry   Country  City

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

Would appreciate your help.

1 Answers

Every time you write the word for you are starting a loop. Try this:

{% 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 %}

Update:

To render it as a table as requested in the comments:

<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>
Related