understood.

But Malcom,

1.i feel doing restructuring of data in views for this purpose is not
appropriate.
In above problem it is obvious like
           1st iteration -----------------1st element in list
            .
            .
            .

  there may be cases where accessing a list may be complex and is not
so abvious.


2.And also It would be better if we can have tag / filter  which
directly gives us an element from the list by taking position as its
input.
   such as..........   {{ list|forloop.counter }}. May be in django
next release.........











On Mar 10, 9:30 am, Malcolm Tredinnick <malc...@pointy-stick.com>
wrote:
> On Mon, 2009-03-09 at 21:21 -0700, Rama Vadakattu wrote:
> > I have a list in template.
> > I need to access that list based on a forloop.counter.
>
> > For example :
>
> > if forloop.counter is 1 then i need to access list[1]
> > if forloop.counter is 5 then i need to access list[5]
> > .
> > so on..........
>
> Django's templates don't support indirect variable lookup like this. You
> need to either write a template filter to do the lookup based on the
> argument you pass in (forloop.counter), or find one on the internet --
> it might well already exist, but I have no idea because I arrange my
> template output to never require it.
>
> Alternatively restructure your data (in your view) so that you can
> iterate over the things you need all at once. For example, if list1 and
> list2 are the objects you're iterating over, change the view to pass
> zip(list1, list2) into the template and then iterate over the combined
> result with:
>
>         {% for val1,val2 in combined_result %}
>            ...
>         {% endfor %}
>
> Regards,
> Malcolm
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to