Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 6 of 10 · Django Templates
Interview question

How do you use {% for %} loops with additional variables like forloop.counter? forloop.counter जैसे additional variables के साथ {% for %} loops कैसे use करें?

Answer

Inside a {% for %} loop, Django provides a special forloop variable exposing metadata about the current iteration - position, first/last flags, and parent loop access for nested loops.

<ul>
{% for book in books %}
    <li>
        {{ forloop.counter }}. {{ book.title }}
        {% if forloop.first %}(First!){% endif %}
        {% if forloop.last %}(Last!){% endif %}
    </li>
{% endfor %}
</ul>

<!-- forloop attributes available:
     forloop.counter     - 1-indexed iteration count (1, 2, 3, ...)
     forloop.counter0    - 0-indexed iteration count (0, 1, 2, ...)
     forloop.revcounter   - counts DOWN to 1
     forloop.revcounter0  - counts DOWN to 0
     forloop.first         - True on the first iteration
     forloop.last          - True on the last iteration
     forloop.parentloop    - access the outer loop's forloop, in nested loops -->

<!-- Nested loop example using forloop.parentloop -->
{% for author in authors %}
    <h3>{{ author.name }}</h3>
    <ul>
    {% for book in author.books.all %}
        <li>{{ forloop.parentloop.counter }}.{{ forloop.counter }} {{ book.title }}</li>
    {% endfor %}
    </ul>
{% endfor %}

<!-- {% empty %} - fallback content when the QuerySet/list is empty -->
{% for book in books %}
    <li>{{ book.title }}</li>
{% empty %}
    <li>No books found</li>
{% endfor %}

<!-- Applying alternating row styles using forloop.counter -->
{% for book in books %}
    <tr class='{% cycle "row-odd" "row-even" %}'>
        <td>{{ book.title }}</td>
    </tr>
{% endfor %}

{% for %} loop के अंदर, Django एक special forloop variable देता है जो current iteration के बारे में metadata देता है - position, first/last flags, nested loops के लिए parent loop access।

<ul>
{% for book in books %}
    <li>
        {{ forloop.counter }}. {{ book.title }}
        {% if forloop.first %}(First!){% endif %}
        {% if forloop.last %}(Last!){% endif %}
    </li>
{% endfor %}
</ul>

<!-- forloop attributes:
     forloop.counter     - 1-indexed count
     forloop.counter0    - 0-indexed count
     forloop.first         - पहली iteration पर True
     forloop.last          - आखिरी iteration पर True
     forloop.parentloop    - nested loops में outer loop -->

{% for author in authors %}
    <h3>{{ author.name }}</h3>
    <ul>
    {% for book in author.books.all %}
        <li>{{ forloop.parentloop.counter }}.{{ forloop.counter }} {{ book.title }}</li>
    {% endfor %}
    </ul>
{% endfor %}

{% for book in books %}
    <li>{{ book.title }}</li>
{% empty %}
    <li>कोई book नहीं मिली</li>
{% endfor %}

{% for book in books %}
    <tr class='{% cycle "row-odd" "row-even" %}'>
        <td>{{ book.title }}</td>
    </tr>
{% endfor %}

Was this answer clear?