Django templates: Simple numeric for loop with only built in components

Scenario:

In your web page, you want to print out a few identical items in a loop.  Perhaps you want 5 rows worth of initial inputs available in a form.

Problem:

Django’s template language has no way to do a simple numeric loop.

Solution:

{% for i in 12345|make_list %}

    {{ i }}

{% endfor %}

Notes:

Clearly this is not beautiful.  But, it works nicely for small cases, and it doesn’t require you to set up any additional template tags or add a range to your context in your view.  I’m sure others have used this approach before, but I couldn’t find anything about it by searching so I thought I’d share.

Comments are closed.