Numeric for loop in Django templates Numeric for loop in Django templates django django

Numeric for loop in Django templates


I've used a simple technique that works nicely for small cases with no special tags and no additional context. Sometimes this comes in handy

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


{% with ''|center:n as range %}{% for _ in range %}    {{ forloop.counter }}{% endfor %}{% endwith %}


Unfortunately, that's not supported in the Django template language. There are a couple of suggestions, but they seem a little complex. I would just put a variable in the context:

...render_to_response('foo.html', {..., 'range': range(10), ...}, ...)...

and in the template:

{% for i in range %}     ...{% endfor %}