Numeric for loop in Django templates

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

    My take on this issue, i think is the nicest. I keep a my_filters.py on the templatetags directory.

    @register.filter(name='times') 
    def times(number):
        return range(number)
    

    And you would use like this:

    {% load my_filters %}
    {% for i in 15|times %}
        
  • Item
  • {% endfor %}

提交回复
热议问题