On 24 Jun 2009, at 12:47 , James Gregory wrote:
> On Jun 24, 11:41 am, James Gregory <james....@gmail.com> wrote:
>> On Jun 24, 11:15 am, "Daniele Procida" <dani...@apple-juice.co.uk>
>> wrote:
>>
>>
>>
>>> I have a template in which I would like to do the following:
>>
>>> {% for item in loop %}
>>>     {% if [previous item].condition %}
>>>         <li>item.name</li>
>>>     {% endif %}
>>> {% endfor %}
>>
>>> In other words, I want to do something with an item in the loop  
>>> based on
>>> whether the *previous* item has some condition set.
>>
>>> Is this possible?
>>
>>> Thanks,
>>
>>> Daniele
>>
>> Quite possibly someone else will give a better solution, but one
>> solution would be to have this in views.py:
>>
>> class MyClass():
>>     condition = False
>>
>> def make_view_list(original_list):
>>     new_list = []
>>     for x in original_list:
>>         new_list.append(x)
>>         if len(new_list) > 1:
>>             new_list[-1].prev_condition = new_list[-2].condition
>>         else:
>>             new_list[-1].prev_condition = False
>>
>>     return new_list
>>
>> def my_view(request):
>>     original_list = [MyClass(),MyClass()]MyClass(),MyClass()]
>>     view_list = make_view_list(original_list)
>>     return render_to_response('index.html', {'list':view_list})
>>
>> James
>
> Actually I imagine "new_list = list(original_list)" would be more
> efficient than doing new_list.append(x) in a loop.
>
> James

Or zip the list with itself (with offsets where needed) and use that,  
either for the transformation or for the display.

--~--~---------~--~----~------------~-------~--~----~
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