Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numeric for loop in Django templates

How do I write a numeric for loop in a Django template? I mean something like

for i = 1 to n 
like image 645
Lev Avatar asked Jul 10 '09 04:07

Lev


People also ask

Can I use range in Django template?

Ans: We can use the range function but there is no range tag or function in Django template.

Which Django template syntax allows to use the for loop?

To create and use for loop in Django, we generally use the “for” template tag. This tag helps to loop over the items in the given array, and the item is made available in the context variable.

What does {% %} mean in Django?

{% %} and {{ }} are part of Django templating language. They are used to pass the variables from views to template. {% %} is basically used when you have an expression and are called tags while {{ }} is used to simply access the variable.


1 Answers

I've used a simple technique that works nicely for small cases with no special tags and no additional context. Sometimes this comes in handy

{% for i in '0123456789'|make_list %}     {{ forloop.counter }} {% endfor %} 
like image 72
Udi Avatar answered Oct 12 '22 22:10

Udi