1

Possible Duplicate:
how to create counter loop in django template?

I want to print some data based on some condition,

I want to use it like we used in other languages:

for(i=1;i<=count;i++)
print i

To do this in django I wrote

{% for i in count %}
<p>{{ i }}</p>
{% endfor %}

but it gives me an error 'int' object is not iterable.Count is coming from views.py and if I prints the count alone than it shows the output.

I wanted to print some value until count not becomes zero,So how i can do this in django.

And one more thing can we use while loop in django because i also try to use it for this task but it gives me the error of invalid block tag: 'while'

So please let me know how can I do this task ?

Thanks

Edit in my view.py I have used like this

count=Product_attributes.objects.count()

and then pass this count to my template

Community
  • 1
  • 1
Inforian
  • 1,716
  • 5
  • 21
  • 42
  • You can do this in django template, please look at older question in SO before posting. Here is one http://stackoverflow.com/questions/5077978/how-to-create-counter-loop-in-django-template – Rohan Oct 10 '12 at 09:58
  • What is count here? Is it a queryset or a list? Only these items are iterable. How do you get it in views? Explain clearly. – arulmr Oct 10 '12 at 09:59
  • I think [Numeric for loop in Django templates](http://stackoverflow.com/questions/1107737/numeric-for-loop-in-django-templates) will help you. – iMom0 Oct 10 '12 at 10:00
  • @arulmr I have edited my question you can see from where i get this count – Inforian Oct 10 '12 at 10:05

3 Answers3

1

Django templates are not programming language. Write all you logic in the view or models, and pass data into the template:

def view(request):
    values = []
    for i in range(10):
         values.append(i) # your custom logic here
    return render_to_response("/path/to/template", {'values': values})

in template:

{% for value in values %}
    <p>{{ value }}</p>
{% endfor %}
defuz
  • 26,721
  • 10
  • 38
  • 60
  • Thanks for your interest. I passed the range(count) to my template and it works for me. Once again thanks – Inforian Oct 10 '12 at 10:12
1

The "for i in var" syntax works only where "var" is an iterable eg a list, tuple, set, dict...

I'd suggest the following: Instead of passing the item count to the template, pass in the iterable eg list in. If all you have is a count, you can create an iterable using range(count) in the view. In code

# Extract from view
def view(request):
    # Set up values. Values is a list / tuple / set of whatever you are counting
    values = Product_attributes.objects.all()
    return render_to_response("/path/to/template", {'values': values})

# Extract from template
{% for value in values %}
   <p>{{value}}</p>
{% endfor %}

The "while" tag is not a valid built in tag in django. A list of valid built-in tags can be seen here: https://docs.djangoproject.com/en/dev/ref/templates/builtins/

This way of doing things is not specific to templates only: it has parallels in "regular python" where the canonical way to iterate over a collection is:

for item in iterable:
    # do something with the item
    pass

More information on the "python way" of doing for loops can be found here: http://wiki.python.org/moin/ForLoop

Ngure Nyaga
  • 2,989
  • 1
  • 20
  • 30
0

If it's not appropriate to add the range to your view code (I don't like adding purely template-ty things to my views), I'd probably create a range filter:

@register.filter
def range(val):
    return range(val)

Then you'd use it like this:

{% for i in count|range %}
<p>{{ i }}</p>
{% endfor %}

There's also an extremely ugly hack you can use if you don't want to bother writing any python code for this, it takes advantage of Django's center (or ljust and rjust) filters which create a string of length of the value provided. You can use it like this:

{% for x in count|center:count %}
<p>{{ forloop.counter }}</p>
{% endfor %}

I wouldn't recommend doing it this way though, I'm just demonstrating that it's possible.

Andrew Ingram
  • 5,160
  • 2
  • 25
  • 37