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


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.


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


{% for i in 12345|make_list %}

    {{ i }}

{% endfor %}


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.