Numeric for loop in Django templates

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

    You should use "slice" in template, a example like this:

    in views.py

    contexts = {
        'ALL_STORES': Store.objects.all(),
    }
    
    return render_to_response('store_list.html', contexts, RequestContext(request, processors=[custom_processor]))
    

    in store_list.html:

      {% for store in ALL_STORES|slice:":10" %}
    • {{ store.name }}
    • {% endfor %}

提交回复
热议问题