Numeric for loop in Django templates

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

    This shows 1 to 20 numbers:

    {% for i in "x"|rjust:"20"|make_list %}
     {{ forloop.counter }}
    {% endfor %}
    

    also this can help you: (count_all_slider_objects come from views)

    {% for i in "x"|rjust:count_all_slider_objects %}
      {{ forloop.counter }}
    {% endfor %}
    

    or

      {% with counter=count_all_slider_objects %}
        {% if list_all_slider_objects %}
          {%  for slide in list_all_slider_objects %}
            {{forloop.counter|add:"-1"}}
            {% endfor%}
          {% endif %}
        {% endwith %}
    

提交回复
热议问题