Numeric for loop in Django templates

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

    Just incase anyone else comes across this question… I've created a template tag which lets you create a range(...): http://www.djangosnippets.org/snippets/1926/

    Accepts the same arguments as the 'range' builtin and creates a list containing
    the result of 'range'.
    
    Syntax:
        {% mkrange [start,] stop[, step] as context_name %}
    
    For example:
        {% mkrange 5 10 2 as some_range %}
        {% for i in some_range %}
          {{ i }}: Something I want to repeat\n
        {% endfor %}
    
    Produces:
        5: Something I want to repeat 
        7: Something I want to repeat 
        9: Something I want to repeat
    
    

提交回复
热议问题