For loop in Django templates

Viewed 56

I want to display the center of a for loop, for example i for loop the 1-30 then i want to for loop the 11-20

here is the example of how i use the for loop in my html:

this is method that i used when i for loop to 30-20

{% for category in category_list reversed %}
{% if forloop.counter < 11 %}
<li><a class="dropdown-item" href="home/category/{{category.name}}">{{ category.name|title }}</a></li>
{% endif %}
{% endfor %}

this i the method when i for loop to 1-10

{% for category in category_list  %}
{% if forloop.counter < 11 %}
<li><a class="dropdown-item" href="home/category/{{category.name}}">{{ category.name|title }}</a></li>
{% endif %}
{% endfor %}

problem how to for loop the 11-20? thanks for the help!

1 Answers

You can add two conditions inside an if block

{% for category in category_list  %}
    {% if forloop.counter >= 11 and forloop.counter <= 20 %}
        <li>
            <a class="dropdown-item" href="home/category/{{category.name}}">{{ category.name|title }}</a>
        </li>
    {% endif %}
{% endfor %}
Related