Categories
Python Answers

How to add a numeric for loop in python Django templates?

Spread the love

To add a numeric for loop in python Django templates, we can add it straight into the template.

For instance, we write

{% for i in '0123456789'|make_list %}
    {{ forloop.counter }}
{% endfor %}

to add a for loop in our template that loops from 0 to 9 since we have in '0123456789' as the loop expression.

We use the make_list filter to convert '0123456789' into a list.

Then we use forloop.counter to get the index.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *