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

Reply via email to