Hi

Benedict Verheyen wrote:
 > When i need such functionality, i assign values to vars of the list items.
 > For instance, in your case i would do something like this
 > def experiments(request):
 >     experiment_list = Experiment.objects.all()
 >     ecount = len(experiment_list) + 1
 >
 >     for e in experiment_list:
 >         e.proc_count = e.experiment_procedure_set.all().count()
 >         return render_to_response('lab/experiments.html',
 >                {'experiment_list': experiment_list,
 >                'ecount': ecount,
 >                })
 >
 > As you've seen, i reuse the experiment_list to loop over and assign
 > values too.
 > This is from the top of my head so i hope it works :)

Yes it does work, thanks heaps. I presume though that the render_to_return is 
outside 
of the previous for loop as below.

for e in experiment_list:
     e.proc_count = e.experiment_procedure_set.all().count()

return render_to_response('lab/experiments.html',
     {'experiment_list': experiment_list,
      'ecount': ecount,
     })

Anyhow with the above I now get the correct list of procs for each experiment. 
So 
what's happening I understand is that I'm creating a new attribute or method of 
the 
object e called .proc_count by simply assigning e.proc_count a value?


> Mike Lake schreef:
>>Im trying to place into a list of experiments the number of procedures for 
>>each experiment.
>>From within the views.py I can save the number of procedures in an experiment 
>>into either a list of tuples (e.id, count) or a dictionary {'e.id': count}. 
>>But when I come to access them in the templates I can't do something like {{ 
>>e."e.id".1 }}
>>
>>The template:
>>
>>This works: {{ pcount.1.1 }} is the number of procedures in experiment 1.
>>This works: {{ pcount.2.1 }} is the number of procedures in experiment 2.
>>
>>{% for e in experiment_list  %}
>>    {{ e.id }}
>>    {{ e.name }}
>>    {{ e."e.id".1 }}  <----- what to use here though ???
>>{% endfor %}
>>
>>In views.py:
>>
>>def experiments(request):
>>    experiment_list = Experiment.objects.all()
>>    ecount = len(experiment_list) + 1
>>
>>    # TODO create a dictionary of the number of procedures in each experiment.
>>    pcount = []
>>    my_tuple = ()
>>    for e in Experiment.objects.all():
>>        my_count = e.experiment_procedure_set.all().count()
>>        pcount.append( (e.id, my_count) )
>>
>>    return render_to_response('lab/experiments.html',
>>            {'experiment_list': experiment_list,
>>             'ecount': ecount, # ecount is the number of experiments.
>>             'pcount': pcount, # pcount is the number of procedures in an 
>> experiment. 
>>            })
>>
>>There are references to 'dynamic lookup' of variables in the docs for 
>>template authors.
>>But this seems very complex to use custom tags and filters. I know the docs 
>>can be a little behind the development version so is there an easy way to do 
>>this?

Mike

-- 
Michael Lake
Computational Research Support Unit
Science Faculty, UTS
Ph: 9514 2238




--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to