Looping....

0 views
Skip to first unread message

Duke

unread,
Apr 15, 2008, 6:36:14 AM4/15/08
to Django users
How to use Loop counter iteration (ie) looping 10 time in Django
Template language (html)

bye
Sathish


Kenneth Gonsalves

unread,
Apr 15, 2008, 6:52:15 AM4/15/08
to django...@googlegroups.com

On 15-Apr-08, at 4:06 PM, Duke wrote:

> How to use Loop counter iteration (ie) looping 10 time in Django
> Template language (html)

use 'for'

--

regards
kg
http://lawgon.livejournal.com
http://nrcfosshelpline.in/code/

Duke

unread,
Apr 15, 2008, 6:53:40 AM4/15/08
to Django users
Can u give some example since for iteration over a array or a
dictionary

Thanks
Duke

Kenneth Gonsalves

unread,
Apr 15, 2008, 7:13:01 AM4/15/08
to django...@googlegroups.com

On 15-Apr-08, at 4:23 PM, Duke wrote:

> Can u give some example since for iteration over a array or a
> dictionary

http://www.djangoproject.com/documentation/templates/#for

Duke

unread,
Apr 15, 2008, 10:50:49 AM4/15/08
to Django users
They are looping over a list
I am looking for

for (i = 0; i < 10; i++) {
printf("Hello, World!);
}
link for looping statement

Thank
Duke

Darryl Ross

unread,
Apr 15, 2008, 11:34:08 AM4/15/08
to django...@googlegroups.com


I am not aware of any tag that will allow you to do that, out of the
box. You have two options, the first is to create a custom template tag
that do what you want[1]. This shouldn't really be terribly difficult to do.

The second option would be to just pass in a variable into the context
with a list containing the number of items of the number of times you
want to loop. Using generic views, this could be done in your urls.py like:

...
('^$', 'direct_to_template',
{ 'template_name': 'homepage.html',
'extra_context': {'looper': range(10) }})
...

Then you can use the standard {% for %} tag:

{% for i in looper %}
{{i}}
{% endfor %}


[1]
http://www.djangoproject.com/documentation/templates_python/#extending-the-template-system

signature.asc

Kenneth Gonsalves

unread,
Apr 15, 2008, 12:07:44 PM4/15/08
to django...@googlegroups.com

where is the use case for this? I cannot conceive of any situation
where one would want to loop over an arbitrary number.

Michael

unread,
Apr 15, 2008, 2:31:01 PM4/15/08
to django...@googlegroups.com
I feel like something like this already exists somewhere, but you can simply write a filter that turns a number into a range:

so in your templates you would have:

{% for i in 10|range %}
...
{%endfor%}

and your template filter would simply be:
from django.template.defaultfilters import stringfilter

@stringfilter
def range(value): return range(int(value))

Kip Parker

unread,
Apr 18, 2008, 11:23:25 AM4/18/08
to Django users
I needed something like this to repeat a part of the template x times,
but I couldn't get this to work, the filter repeatedly returned the
range(0,10) so gave a recursion depth error. I may have set it up
wrong.

I ended up making a repeat tag:

def do_repeat(parser, token):
try:
# Splitting by None == splitting by spaces.
tag_name, arg = token.contents.split(None, 1)
number = int(arg)
except ValueError:
raise template.TemplateSyntaxError, "Repeat tag requires exactly one
argument which must be a number"
nodelist = parser.parse(('endrepeat',))
parser.delete_first_token()
return RepeatNode(nodelist, number)

class RepeatNode(template.Node):
def __init__(self, nodelist, number):
self.nodelist = nodelist
self.number = number
def render(self, context):
output = self.nodelist.render(context)
return output*self.number
register.tag('repeat', do_repeat)

but it really feels like it shouldn't be that hard. Maybe I missed the
easy way?

Eric Abrahamsen

unread,
Apr 18, 2008, 12:47:18 PM4/18/08
to django...@googlegroups.com
I think what people are saying here is that your number, the iteration
limit, has to be coming from somewhere or something. Chances are, that
something is an iterable, or can be made into an iterable very easily,
and thus can be used in a for loop. Where is the number coming from?

alex....@gmail.com

unread,
Apr 18, 2008, 1:56:03 PM4/18/08
to Django users
The way I would do it is to create a tag that creates a list object
using range as a template var and then use a simple for loop, the
usage would be:

{% range 1 10 as my_range %}
{% for i in my_range %}
{{ i }},
{% endfor %}
would return 1, 2, 3, 4, 5, 6, 7, 8, 9,

I'm going to write this up and post it to django snippets later, if
anyone wants it, I'll post a link.
> >>>http://nrcfosshelpline.in/code/- Hide quoted text -
>
> - Show quoted text -

Erik Vorhes

unread,
Apr 18, 2008, 2:00:46 PM4/18/08
to django...@googlegroups.com
Or you can add something to your view to take care of this:

def your_view(request):
...
some_subset = Model.objects.all()[:10]
...

Add it to 'extra_context' and you won't need to do anything trickier than this:

{% for model in some_subset %}
some stuff
{% endfor %}

--
portfolio: http://textivism.com/
blog: http://erikanderica.org/erik/

alex....@gmail.com

unread,
Apr 18, 2008, 2:28:05 PM4/18/08
to Django users
Here's the code for the range tag I wrote: http://dpaste.com/45711/ I
haven't tested it at all, but I think it should work, if anyone can
provide feedback I'll post it on django snippets.

On Apr 18, 2:00 pm, "Erik Vorhes" <e...@textivism.com> wrote:
> Or you can add something to your view to take care of this:
>
> def your_view(request):
> ...
> some_subset = Model.objects.all()[:10]
> ...
>
> Add it to 'extra_context' and you won't need to do anything trickier than this:
>
> {% for model in some_subset %}
> some stuff
> {% endfor %}
>
> On Fri, Apr 18, 2008 at 12:56 PM, alex.gay...@gmail.com
> > > >>>http://nrcfosshelpline.in/code/-Hide quoted text -
Reply all
Reply to author
Forward
0 new messages