lookup dictionary in django template

How to Lookup Dictionary Value with Key in Django Template

Sometimes you may need to acccess dictionary elements or find dictionary value using key in Django template. If you do not use the right syntax, your template will throw an error. In this article, we will look at the different ways to lookup dictionary value with key in Django template.

How to Lookup Dictionary Value with Key in Django Template

Here are the different ways to do a dictionary lookup in Django template.

1. Lookup using String as Key

If are looking up using key values directly, then you can use any of the following notations to get the dictionary value.


For example if you have the following view

def test(request):
    data = {'key1':'value1','key2':'value2'}
    return render(request, 'page.html',{'data':data})    

In your Django template page.html you can lookup dictionary data by directly using key names.

<p>Value of Key 1 : data["key1"]</p>

2. Lookup using Variable as Key

However, if you want to lookup dictionary values using a variable as key name then you cannot use the above notation. For example, let us say you have the following dictionary.

data = {'a': 1, 'b': 2,'c':3 }

if you use the code below, Django will give you an error.

{% for key in data.items %}
   <p>{{ data[key] }}</p>
{% endfor %}

This is because in Django you need to use a literal string such as “a”, or “b” as key when you lookup a dictionary in Django template. You cannot use a variable name as key.

In this case, there are two ways to solve the problem. First, loop through the data instead of data.items() and fetch both key as well as value in each iteration of your dictionary, as shown below.

{% for key, value in data %}
   <p>Key {{ key }}, Value {{ value }}</p>
{% endfor %}

In this case you can directly use the key and value together without doing a lookup. Since you know the key value in each iteration, its corresponding value will be stored in value loop variable.

The second method requires you to create a template filter as shown below.

from django.template.defaulttags import register
def get_value(dictionary, key):
    return dictionary.get(key)

We create a get_value template filter that basically accepts a dictionary & key, does the lookup and returns the value. Please note, we have used dictionary.get(key) instead dictionary[key] to do the lookup. This ie because if you use dictionary.get(key) then Django will return null value if key is null. If you use dictionary[key] and key is null, then Django will throw an error.

Now you can lookup dictionary with key as variable.

{% for item in data.items %}
  <p>{{ data|get_value:item.NAME }}</p>
{% endfor %}

In this article, we have provided a couple of ways to access dictionary elements in Django templates. Depending on your use case, you can decide which one to use. Please remember that you cannot use a variable as a key while doing dictionary lookups in Django template. For that, you need to define a template filter as shown above and only they you will be able to access dictionary elements using loop variables.

Also read :

How to Loop Through List in Django Template
How to Run Python Script in Django Project
How to Disable CSRF Validation in Django View
How to Enable CORS in Django Project
How to Combine Querysets in Django