Numeric for loop in Django templates

后端 未结 19 862
温柔的废话
温柔的废话 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:04

    You can pass a binding of

    {'n' : range(n) }
    

    to the template, then do

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

    Note that you'll get 0-based behavior (0, 1, ... n-1).

    (Updated for Python3 compatibility)

提交回复
热议问题