Распределить два массива по таблице
Необходимо распределить два массива по таблице, каждый в своём столбце. Понимаю, что вопрос очень глупый и простой, но никак не могу справиться с этим :( Заранее спасибо!
<table class="table table-striped" cellpadding="0" cellspacing="0">
<thead>
<tr>
<th>#</th>
<th>Site Title</th>
<th>Url</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for q in q %}
{% for z in z %}
<tr>
<td class="counterCell"></td>
<td>{{q}}</td>
<td>{{z}}</td>
</tr>
{% endfor %}
{% endfor %}
</tbody>
</table>
Результат (таблица):
Los Angeles Times | www.latimes.com
Los Angeles Times | www.bbc.com
Los Angeles Times | www.abcnews.go.com
BBC | www.latimes.com
BBC | www.bbc.com
BBC | www.abcnews.go.com
ABC News | www.latimes.com
ABC News | www.bbc.com
ABC News | www.abcnews.go.com
Нужно:
Los Angeles Times | www.latimes.com
BBC | www.bbc.com
ABC News | www.abcnews.go.com
ОБНОВЛЕНО:
cursor = connection.cursor()
cursor1 = connection.cursor()
cursor.execute("SELECT sites.site_title, sites.url FROM sites, my_sites, auth_user WHERE auth_user.id = my_sites.id_user AND sites.id = my_sites.id_site AND auth_user.id ="+str(request.user.id))
cursor1.execute("SELECT sites.site_title, sites.url FROM sites, my_sites, auth_user WHERE auth_user.id = my_sites.id_user AND sites.id = my_sites.id_site AND auth_user.id ="+str(request.user.id))
q = [str(row[0]) for row in cursor.fetchall()]
z = [str(row[1]) for row in cursor1.fetchall()]
#print(q)
#print(z)
#print(q+z)
a = [(q,z)]
print(a)
return render(request, 'main/my_newsagent.html', {'q': q, 'z': z})
Результат: [(['Los Angeles Times', 'BBC', 'ABC News'], ['www.latimes.com', 'www.bbc.com', 'www.abcnews.go.com'])]
Ответы (1 шт):
Типо того попробуйте:
views.py
q = [str(row[0]) for row in cursor.fetchall()]
z = [str(row[1]) for row in cursor1.fetchall()]
qz = [(q[i], z[i]) for i in range(len(q))]
return render(request, 'main/my_newsagent.html', {'qz': qz})
my_newsagent.html
<tbody>
{% for element in qz %}
<tr>
<td class="counterCell"></td>
<td>{{ element.0 }}</td>
<td>{{ element.1 }}</td>
</tr>
{% endfor %}
</tbody>