Mike Lake schreef:
> Hi all
>
> 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?
>
> Michael Lake
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 :)
Regards,
Benedict
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---