How to Display Index During List Iteration With Django

How to display index during list iteration with Django?

You want the forloop.counter template variable.

The for loop sets a number of variables available within the loop:

forloop.counter The current iteration of the loop (1-indexed)

So your code would look like:

{% for sport in sports %}
<p>{{forloop.counter}}. {{ sport }}</p>
{% endfor %}

Of course, for simply displaying the data as you do in your question, the easier way is just to show the items in an ordered list, and the browser will do the numbering for you:

<ol>
{% for sport in sports %}
<li>{{ sport }}</li>
{% endfor %}
</ol>

How do I iterate and index a list in Django template

Perhaps you want get key and value in template

{% for key, value in your_dict.items %}

{{ key }}-{{ value }}

{% endfor %}

Your question already at here.

Django print loop index inside List

try following

{% for i in data%}
<td id ='b{{forloop.counter0}}'>
Data is{{ i.B }}
</td>
{% endfor %}

Django - iterate number in for loop of a template

Django provides it. You can use either:

  • {{ forloop.counter }} index starts at 1.
  • {{ forloop.counter0 }} index starts at 0.

In template, you can do:

{% for item in item_list %}
{{ forloop.counter }} # starting index 1
{{ forloop.counter0 }} # starting index 0

# do your stuff
{% endfor %}

More info at: for | Built-in template tags and filters | Django documentation

ForLoop Iterations as List Index in Django Template

I'd say that the best approach would be to work on the data in the view before passing it to the template.

For example, the zip builtin can be used to create the rows list based on the lists that contain the columns:

rows = zip(list1, list2, list3, list4)

After that, the template can iterate over the rows one by one and access the columns using index access if needed:

{% for row in rows %}
{{row.0}} {{row.1}} {{row.2}} {{row.3}}
{% endfor %}

Django - get element index in a list in template

{{forloop.counter}} this is it.
Only add this line before the element

Using index from iterated list

It sounds like you want to iterate over two lists at the same time, in other words zip() lists.

If this is the case, it is better to do this in the view and pass inside the context:

headers = ["X", "Y", "Z", "XX", "YY"]
data = zip(headers, myarray.all())
return render(request, 'template.html', {'data': data})

Then, in the template:

{% for header, row in data %}
<tr>
<th>{{ header }}</th>
<td>{{ row }}</td>
</tr>
{% endfor %}

Iteratively calling the index of a list in Django template forloop

You don't need ids

This:

{% for image in images %}  
<div id ="img{{ forloop.counter }}"><img src="{{ image.url }}"></div>
{% endfor %}

will output:

<div id ="img1"><img src="<url>"></div>
<div id ="img2"><img src="<url>"></div>
.......

forloop.counter The current iteration of the loop (1-indexed)

forloop.counter0 The current iteration of the loop (0-indexed)

Django/Jinja2: How to use the index value in a for-loop statement to display two lists?

A better option is to create a zip object in your view, to match the pet to a price, and pass only this to the template :

def display(request):
listone = ['duck', 'chicken', 'cat', 'dog']
listtwo = ['4 dollars', '3 dollars', '2 dollars', '1 dollars']
fusion = zip(listone, listtwo)
return render(request, 'display.html', {'fusion' : fusion})

Then you can unpack it in your template, zip() will produce a list of tuples (name, price) and you can easily loop on this :

<table>
<tr>
<th>Pet</th>
<th>Price</th>
</tr>
{% for name, price in fusion %}
<tr>
<td>{{ name }}</td>
<td>{{ price }}</td>
</tr>
{% endfor %}
</table>

Another solution is to create a new custom template filter, there are already some posts explaining the process : https://stackoverflow.com/a/29664945/6655211



Related Topics



Leave a reply



Submit