[Answered ]-Django Custom Filter Tags and access query

1👍

You can use the {% with … %} … {% endwith %} template tag [Django-doc]:

{% with somevar=dict_post|get_item:1 %}
    {{ somevar.title }}
{% endwith %}

since we here know that the key is 1 (likely that will later change), we can work with:

{# if the key is known to be 1 #}
{{ dict_post.1.title }}

That being said, normally it is better to "prepare" the data in the view in such way that you do not need to perform dictionary lookups with a variable key. The Django template language is deliberately restricted to prevent people from writing business logic in the template.

Leave a comment