how to create counter loop in django template?

后端 未结 3 1576
無奈伤痛
無奈伤痛 2021-01-29 12:28

can any tell me how can I write below code of c in django

for(c=0; c<5; c++)
  //do something

i had tried below code but it gives me an erro

相关标签:
3条回答
  • 2021-01-29 12:46

    I guess you are not good at searching, right (:

    A good documentation for a good framework... On the other hand, why you ask for a c-like loop structure in a framework written for python is another question

    EDIT: For loop in django templates iterates through an array (or list in python terms). So you have to have a list to iterate.. In your related view, lets say yopu have a number list

    number_list = [1,2,3,4,5]
    

    if you pass this list to the template with the same name, then you can iterate through it with

    {%for num in nuber_list%}
        Number is : {{num}}
    {%endfor%}
    

    But as i said, you have to pass that list to template in the return statement line that returns an httpresponse or render your contect to your template as it described in here

    0 讨论(0)
  • 2021-01-29 12:55

    I was curious and found a way to do this. Disclaimer: I consider the following code to be WRONG:

    {% for i in "abcde" %} do something {% endfor %}
    

    Replace "abcde" with a string of the range you want.

    0 讨论(0)
  • 2021-01-29 13:00

    When you render your template, you may pass range

    render_to_response('template_x.html', {'range5': range(5)})
    

    And in html template, probably like this

    {% for i in range5 %}
        <div class="tab-content">
        <h1 class="tab" title="title for page {{i}}">Page {{i}}</h1>
        <p>This is the content of tab {{i}} on container {{i}}</p>
        </div>  
    {% endfor %}
    
    0 讨论(0)
提交回复
热议问题