382

I have the following for loop in my django template displaying days. I wonder, whether it's possible to iterate a number (in the below case i) in a loop. Or do I have to store it in the database and then query it in form of days.day_number?

{% for days in days_list %}
    <h2># Day {{ i }} - From {{ days.from_location }} to {{ days.to_location }}</h2>
{% endfor %}
orschiro
  • 19,847
  • 19
  • 64
  • 95

7 Answers7

885

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

daaawx
  • 3,273
  • 2
  • 17
  • 16
Rohan
  • 52,392
  • 12
  • 90
  • 87
159

Also one can use this:

{% if forloop.first %}

or

{% if forloop.last %}
JMJ
  • 2,034
  • 2
  • 16
  • 17
  • 23
    Not the answer to the question, but still the answer for many people that will search for this question. Good stuff! – kontur Dec 03 '18 at 13:24
  • Great answer. Helped a lot. Just got an exception because I was using Jinja 2. In case you have same issue, with Jinja 2 you can use {{ loop.index }} (indexing starts at 1) – Giorgi Patsatsia Apr 29 '23 at 16:53
16
{% for days in days_list %}
    <h2># Day {{ forloop.counter }} - From {{ days.from_location }} to {{ days.to_location }}</h2>
{% endfor %}

or if you want to start from 0

{% for days in days_list %}
        <h2># Day {{ forloop.counter0 }} - From {{ days.from_location }} to {{ days.to_location }}</h2>
{% endfor %}
Ahmed Elgammudi
  • 642
  • 7
  • 15
  • I upvoted because it shows how to specifically answer the question. Although it is not a general ansswer, it gives an example on how to use it. – deps_stats Nov 01 '21 at 18:26
9

from the docs https://docs.djangoproject.com/en/stable/ref/templates/builtins/#for you can found it to count items you can use a counter like this

{% for job in jobs %}
<td>{{ forloop.counter }}</td>
<td>{{ job.title }}</td>
<td>{{ job.job_url }}</td>
{% endfor %}
  • {{ forloop.counter }} start counting from 1
  • {{ forloop.counter0 }} start counting from 0
Lars
  • 1,234
  • 1
  • 8
  • 31
perymerdeka
  • 766
  • 9
  • 19
3

Do like this,

{% for days in days_list %}
    <h2># Day {{ forloop.counter }} - From {{ days.from_location }} to {{ days.to_location }}</h2>
{% endfor %}
Travis
  • 103
  • 8
1

[Django HTML template doesn't support index as of now], but you can achieve the goal:

If you use Dictionary inside Dictionary in views.py then iteration is possible using key as index. example:

{% for key, value in DictionartResult.items %} <!-- dictionartResult is a dictionary having key value pair-->
<tr align="center">
    <td  bgcolor="Blue"><a href={{value.ProjectName}}><b>{{value.ProjectName}}</b></a></td>
    <td> {{ value.atIndex0 }} </td>         <!-- atIndex0 is a key which will have its value , you can treat this key as index to resolve-->
    <td> {{ value.atIndex4 }} </td>
    <td> {{ value.atIndex2 }} </td>
</tr>
{% endfor %}

Elseif you use List inside dictionary then not only first and last iteration can be controlled, but all index can be controlled. example:

{% for key, value in DictionaryResult.items %}
    <tr align="center">
    {% for project_data in value %}
        {% if  forloop.counter <= 13 %}  <!-- Here you can control the iteration-->
            {% if forloop.first %}
                <td bgcolor="Blue"><a href={{project_data}}><b> {{ project_data }} </b></a></td> <!-- it will always refer to project_data[0]-->
            {% else %}
                <td> {{ project_data }} </td> <!-- it will refer to all items in project_data[] except at index [0]-->
            {% endif %}
            {% endif %}
    {% endfor %}
    </tr>
{% endfor %}

End If ;)

// Hope have covered the solution with Dictionary, List, HTML template, For Loop, Inner loop, If Else. Django HTML Documentaion for more methods: https://docs.djangoproject.com/en/2.2/ref/templates/builtins/

Mayur Raj
  • 61
  • 5
0

I think you could call the id, like this

{% for days in days_list %}
    <h2># Day {{ days.id }} - From {{ days.from_location }} to {{ days.to_location }}</h2>
{% endfor %}