-2

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 error

{% for(c=0; c<5; c++)%}
     <div class="tab-content">
    <h1 class="tab" title="title for page 1">Page 1</h1>
    <p>This is the content of tab 1 on container 1</p>
     </div>     
{% endfor %}
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
mahesh
  • 4,625
  • 11
  • 42
  • 61
  • 4
    -1, at least. What on earth makes you think the Django template language is C? And this is all very clearly documented in the Django reference. – Daniel Roseman Feb 22 '11 at 12:20
  • but i m getting error that is the reason I have to ask such silly que.. anyway if u can write this example then plzz suggest me – mahesh Feb 22 '11 at 12:24
  • 3
    But why can't you look at the very good documentation? What's wrong with what you find there? – Daniel Roseman Feb 22 '11 at 12:26
  • I had tried many examples given on web such as example given in http://code.djangoproject.com/ticket/5172 but I dont kn why I am getting error in these examples, and reason to write such question is reader can easily understand my question – mahesh Feb 22 '11 at 12:32
  • That's a feature request, not documentation. Read what the header of the page says before using the examples. – Rosh Oxymoron Feb 22 '11 at 12:57
  • @mahesh, you might want to take a look at this custom tag snippet - http://djangosnippets.org/snippets/1357/ – YOU Feb 22 '11 at 12:59
  • That ticket is not just a feature request, it's a feature request that's been closed as "not going to do it". It's a wish denied. – Ned Batchelder Feb 22 '11 at 13:09
  • Django's documentation is one of it's greatest features. Here's your for loop from the docs: http://docs.djangoproject.com/en/1.2/ref/templates/builtins/#for – RyanBrady Feb 22 '11 at 13:37

3 Answers3

4

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 %}
YOU
  • 120,166
  • 34
  • 186
  • 219
3

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

Mp0int
  • 18,172
  • 15
  • 83
  • 114
3

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.

ojii
  • 4,729
  • 2
  • 23
  • 34