45๐
If both lists are of the same length, you can return zipped_data = zip(table, total)
as template context in your view, which produces a list of 2-valued tuples.
Example:
>>> lst1 = ['a', 'b', 'c']
>>> lst2 = [1, 2, 3]
>>> zip(lst1, lst2)
[('a', 1), ('b', 2), ('c', 3)]
In your template, you can then write:
{% for i, j in zipped_data %}
{{ i }}, {{ j }}
{% endfor %}
Also, take a look at Djangoโs documentation about the for
template tag here. It mentions all possibilities that you have for using it including nice examples.
7๐
For any recent visitors to this question, forloop.parentloop can mimic the zipping of two lists together:
{% for a in list_a %}{% for b in list_b %}
{% if forloop.counter == forloop.parentloop.counter %}
{{a}} {{b}}
{% endif %}
{% endfor %}{% endfor %}
- [Django]-Get list item dynamically in django templates
- [Django]-Uninstall Django completely
- [Django]-How to get getting base_url in django template
5๐
If itโs just the variables i
and j
that youโre looking at then this should work โ
return render_to_response('results.html',
{'data': zip(table, list)})
{% for i, j in data %}
<tr>
<td> {{ i }}: </td> <td> {{ j }} </td>
</tr>
{% endfor %}
(credit to everyone else who answered this question)
- [Django]-What does 'many = True' do in Django Rest FrameWork?
- [Django]-Check if celery beat is up and running
- [Django]-Django 1.7 โ App 'your_app_name' does not have migrations
5๐
Use pythonโs zip function and zip the 2 lists together.
In your view:
zip(table, list)
In your template, you can iterate this like a simple list, and use the .0 and .1 properties to access the data from table and list, respectively.
- [Django]-Disable session creation in Django
- [Django]-Django connection to postgres by docker-compose
- [Django]-Get list item dynamically in django templates
1๐
Rather than using a dictionary (which does not guarantee any kind of sorting), use the python zip
function on the two lists and pass it to the template.
- [Django]-How to serve media files on Django production environment?
- [Django]-Django Generic Views using decorator login_required
- [Django]-Django-nonrel + Django-registration problem: unexpected keyword argument 'uidb36' when resetting password
1๐
Youโll have to do this in the view โ use the builtin zip function to make a list of tuples, then iterate over it in the template.
Template logic is purposely simple, anything even slightly complex should be done in the view.
- [Django]-Allowing RabbitMQ-Server Connections
- [Django]-Getting Values of QuerySet in Django
- [Django]-Where to put business logic in django