this is my code :
{% for i in range(7)%}
<option value={{i+1}}> {{i+1}}</option>
{% endfor %}
but it show error ,
what can i do ,
thanks
{% %} 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.
Yes you can extend different or same templates.
Django for loop counter All the variables related to the counter are listed below. forloop. counter: By using this, the iteration of the loop starts from index 1. forloop. counter0: By using this, the iteration of the loop starts from index 0.
For loop in Django template templates tag for-loop 0 49201 For loop is used to iterate over any iterable object, accessing one item at a time and making it available inside the for loop body. For example, if you want to create a drop down of countries in Django template, you can use the below code.
Here is a simple way to loop through it in your Django template page.html In the above code, we loop through the ID list returned by our Django view. You can refer to each item directly within { { }}.
There is no break statement in Django template For loop. Depending on your requirement you can do one of the following. Option 1 - Iterate over the whole list but do not perform any action if the condition is not matched. For example, you are printing numbers from a list and you need to exit the list as soon as number 99 is encountered.
In such cases item at the current index doesn't matter. In python you would use range function. But again, there is no range tag or function in Django template. You can use either one of the below approaches. Option 1 - Pass the list to render function along with template.
In python strings are iterables so this works :
{% for i in "1234567" %}
<option value={{i}}> {{i}}</option>
{% endfor %}
It's explicit, so quite OK, but zjm1126's answer is probably better for long term consideration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With