Numeric for loop in Django templates

后端 未结 19 886
温柔的废话
温柔的废话 2020-11-22 03:29

How do I write a numeric for loop in a Django template? I mean something like

for i = 1 to n
19条回答
  •  后悔当初
    2020-11-22 04:12

    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 %}
    

提交回复
热议问题