Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to loop 7 times in the django templates [duplicate]

Tags:

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

like image 882
zjm1126 Avatar asked Mar 09 '11 07:03

zjm1126


People also ask

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.

Do Django templates have multiple extends?

Yes you can extend different or same templates.

What is Forloop counter in Django?

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.

What is the use of for loop in Django?

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.

How do I loop through a list in Django view?

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 { { }}.

How do you break a for loop in Django?

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.

How to range a list in a Django template?

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.


1 Answers

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.

like image 166
blobmaster Avatar answered Oct 01 '22 12:10

blobmaster