how to run this code in django template how to run this code in django template python python

how to run this code in django template


The template subsystem has some special constructs built into the for/endfor block that allows you to access the current index of the loop without having to call enumerate.

{% for j in a %}    {{ forloop.counter0 }}, {{ j }}{% endfor %}

While this snippet solves your immediate problem, if you're expecting to have access to Python builtins and other Python constructs inside your Django templates, you may be misunderstanding the sandbox that it provides/enforces.


you can use {{ forloop.counter }} or {{ forloop.counter0 }} for the same effect, the latter is 0-indexed, thus more like enumerate.


{% for item in a %}    {{ forloop.counter }}, {{ item }}{% endfor %}

Link related