On Thu, May 5, 2011 at 12:11 PM, AJ <amanjsi...@gmail.com> wrote:
> I see. Any clue where I can begin in my case (for filters etc.)? Thanks.
>

I disagree with the BDFLs on this, for me this pattern is very natural:

{% for key in list_of_keys %}
{{ some_dictionary[key] }}
{{ some_other_dict_with_same_keys[key] }}
{% endfor %}

Obviously this syntax is not allowed, and, as you've found out, key
lookup only works for numeric or string literals.

Therefore, I use a trivial filter tag:

{% load dict_tags %}
{% for key in list_of_keys %}
{{ some_dictionary|get:key }}
{{ some_other_dict_with_same_keys|get:key }}
{% endfor %}

some_app/templatetags/dict_tags.py:

from django import template
register = template.Library()
@register.filter
def get(the_dict, key):
  return the_dict[key]

Cheers

Tom

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