Re: How to do dynamic lookup of variables.

2007-02-15 Thread Benedict Verheyen

Michael Lake schreef:

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

Yes it's outside the loop as you already found out.

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

Yes indeed, that's what happens. It's an easy way to "transport" extra
data to your templates.

Glad it worked.

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



Re: How to do dynamic lookup of variables.

2007-02-15 Thread jfagnani

On Feb 14, 4:41 pm, Mike Lake <[EMAIL PROTECTED]> wrote:

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

A custom lookup filter is quite easy to do, because the template
module already has a lookup function. All you need to do is expose it
as a filter:

from django import template

register = template.Library()

def lookup(value, key):
return template.resolve_variable(key,value)

register.filter(lookup)

> {{ e."e.id".1 }}  <- what to use here though ???

You case is a little different than what I've use this for because you
want to do a lookup on the result of the lookup, one of which would be
handled normally by the template, the other you need a filter for. I'm
not sure if you can use the dot syntax on the result of a filter, but
can chain filters, so you could write:

{{ e|lookup:e.id|lookup1 }}

But this statement looks a little weird to me given what's in your
view code. I think you probably meant {{ p_count."e.id".1 }}. Since
p_count is a list, I'm not even sure that would work. I'd use a dict,
like you mentioned earlier so that you view is something like:

for e in Experiment.objects.all():
my_count = e.experiment_procedure_set.all().count()
pcount[e.id] = my_count

And you template lookup was like:

{{ p_count|lookup:e.id }}


I think the filter syntax is really unfortunate, since precedence can
be hard to determine when using both filters and dot syntax lookups,
and filters can only take one parameter. Parenthesized expressions
would be a lot better. They're common for a reason.

Hope this helped,
  Justin


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



Re: How to do dynamic lookup of variables.

2007-02-15 Thread Michael Lake

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



Re: How to do dynamic lookup of variables.

2007-02-14 Thread Benedict Verheyen

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