Problems with Custom Tag Filter

4 views
Skip to first unread message

Mark Phillips

unread,
Jan 16, 2014, 5:43:15 PM1/16/14
to django users
I have a dictionary of dictionaries that I need to open up in a template.

The dictionary:
'values': {(1L, 2L): {'selling_costs__sum': None, 'sale_price__sum': None, 'debt__sum': None, 'estimate__quote__sum': None}, 
(3L, 2L): {'selling_costs__sum': Decimal('10.45'), 'sale_price__sum': Decimal('50.00'), 'debt__sum': Decimal('0.00'), 'estimate__quote__sum': Decimal('100.00')}}

My templatetag:
from django import template
register = template.Library()

@register.filter
def lookup(container, key):
    if type(container) is dict:
        return container.get(key)
    elif type(container) in (list, tuple):
        return container[key] if len(container) > key else None
    return None

And my template code:
{% for location in locations %}
    {% for category in categories %}
<tr>
    <td>{{ location }}</td>
    <td>{{ category }}</td>
    <td>${{ values|lookup:(location.id, category.id)|lookup:'selling_costs__sum' }}</td>
</tr>
    {% endfor %}
{% endfor %}

The error I get is: lookup requires 1 arguments, 0 provided.

The location.id and category.id both generate the correct values. It seems that template tags can't take a tuple as a key for a dictionary. 

Also I read that one cannot chain a filter like this. Is this true?

Any suggestions on how to read data from the dictionary values?

Thanks!

Mark
Reply all
Reply to author
Forward
0 new messages