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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---