Django’s template system provides a powerful way to generate dynamic HTML pages. One common task is accessing values from a Python dictionary passed into the template context. With Django’s template tags and filters, accessing and outputting dictionary values is straightforward. In this post, we’ll go over the different methods for accessing dictionary elements in Django templates.
The Basics of Django Templates
First, a quick overview of Django templates. Templates are HTML files with template tags and variables. The template tags allow you to insert dynamic content and control logic like for loops and if statements. The variables get populated from the view code that renders the template.
For example, in a view you might have:
context = {
'name': 'John',
'age': 25
}
return render(request, 'index.html', context)
Then in the index.html template, you could output John’s name and age like:
<p>Name: {{ name }}</p>
<p>Age: {{ age }}</p>
The template tags {{ name }}
and {{ age }}
get replaced by the actual values from the context dictionary.
Accessing Dictionary Elements
To access an element in a dictionary passed to the template, you use the dot notation. For example, if the context was:
context = {
'person': {
'name': 'Mary',
'age': 32
}
}
In the template you would use:
<p>Name: {{ name }}</p>
<p>Age: {{ age }}</p>
This outputs the name and age values from the nested `person` dictionary.
You can also use variable names in brackets like:
<p>{{ person['name'] }}</p>
This does the same thing, accessing the name key of the person dictionary.
Using the with Templates Tag
When accessing multiple values from a nested dictionary, you can clean up your code by using Django’s `with` template tag.
For example:
{% with person=person %}
<p>{{ person.name }}</p>
<p>{{ person.age }}</p>
{% endwith %}
This assigns the `person` dictionary to a local variable named person that we can then access more easily.
Default Values with `|default`
If a key might be missing from the dictionary, you can provide a default fallback value using the `default` filter.
For example:
<p>{{ person.name|default:"Unknown" }}</p>
This will output the value of `person.name` or “Unknown” if that key is not present in the dictionary.
Checking Key Existence with `if`
You can check if a key is in a dictionary using Django’s `if` template tag.
For example:
{% if "name" in person %}
<p>{{ person.name }}</p>
{% else %}
<p>No name</p>
{% endif %}
This outputs the name if present, otherwise prints a default “No name” message.
Accessing Keys with Variables in Templates
You can also use a template variable itself as the dictionary key.
For example:
{% with key='name' %}
<p>{{ person.{{ key }} }}</p>
{% endwith %}
Here `key` would contain the string `”name”` which gets interpolated into the person dictionary lookup.
Outputting All Keys
To output all the keys in a dictionary, you can use the `dict.items` attribute in a for loop:
{% with key='name' %}
<p>{{ person.{{ key }} }}</p>
{% endwith %}
This will loop through each key/value pair and output them in an HTML list.
Summary
Accessing dictionary elements is straightforward in Django templates. The main points covered:
- Use dot notation like
dict.key
to access values - The
with
tag can simplify accessing nested dictionaries - Provide default values with the
|default
filter - Check key existence with
{% if "key" in dict %}
- Interpolate variables into the key name like
{{ dict.{{var}} }}
- Loop through all keys with
{% for key, value in dict.items %}
Django’s template system gives you several options for cleanly accessing dictionary data. With some practice it becomes very natural to work with dictionaries passed into templates. The Django built-in tags and filters provide you additional functionality beyond basic dictionary lookups.