On 9/5/2010 7:54 AM, Jagdeep Singh Malhi wrote:
>> I have question about "for loop" used in Templates the correct
>> Syntax of using For Loop in template is : {% for X in VALUE %}
>> 
>> but I want to use For loop with two value Is it possible to use two
>> value in one loop? For example : {% for X, Y   in   VALUE1, VALUE2
>> %} Or {% for X in VALUE1  && Y in VALUE2 %}
>> 
>> These syntaxes are wrong, just for example. Is it possible to use
>> for loop  with two value in django templates? if yes, How?
>> 
>> Thanks
>> 
If these objects are lists, say list1 and list2, in your view just
create a single object made up of pairs of elements from the original lists:

   values = zip(list1, list2)

Then you can pass that item to your template and iterate over it.

In older Django you would have to use dot notation to select the elements:

     {% for x in VALUES %}
        First item is x.0
        Second item is x.1
     {% endfor %}

I believe in more recent versions you have the option of unpacking the
two elements in the for statement:

    {% for x1, x2 in lists %}
        ...
    {% endfor %}

but don't take that as gospel until you've tried it.

regards
 Steve

-- 
DjangoCon US 2010 September 7-9 http://djangocon.us/

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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