dictionary in template tag django

Viewed 89

I would like to extract something like bellow:

{% for item in query_ %}
 <td>{{ mydictionary[item.id] }}</td>
{% endfor %}

which of course gives me a syntax error. For this purpose I have defined a template tag

@register.filter('get_value_from_dict')
def get_value_from_dict(dict_data, key):
    if key:
        return dict_data.get(key)

and then use it as follows:

{% for item in query_ %}
 <td>{{ mydictionary | get_value_from_dict : item.id }}</td>
{% endfor %}

however it doesnt work and gives me the following error:

get_value_from_dict requires 2 arguments, 1 provided

I was wondering what I am doing wrong.

1 Answers

This is an issue with the spacing. You should not add spaces around the colon (:), you thus can fix this with:

{{ mydictionary|get_value_from_dict:item.id }}
Related