How to access a dictionary element in a Django template?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Django templates can access dictionary values, but the syntax is more limited than normal Python code. The template language is intentionally restrictive, so you cannot freely write arbitrary expressions such as bracket indexing inside the template.
For simple keys, dot lookup is often enough. For dynamic keys or awkward dictionary shapes, the usual fix is to prepare the value in the view or add a small custom template filter.
Use Dot Lookup for Simple Keys
If the dictionary key is a simple string, Django will often resolve it with dot syntax:
In the template:
For common cases, that is all you need. Django tries dictionary lookup before attribute lookup in many template resolution paths, which is why this works.
Why Bracket Syntax Does Not Work
Python code like my_dict["name"] is not valid Django template syntax. Templates are designed for presentation, not general-purpose programming, so indexing expressions are intentionally constrained.
If you need a literal key that is not a clean identifier, or a variable key chosen at runtime, dot lookup becomes unreliable or impossible.
Pass the Final Value from the View
The simplest solution for dynamic access is often to resolve the dictionary in Python before rendering:
Template:
This keeps business logic out of the template and is usually the cleanest answer.
Add a Custom Filter for Dynamic Keys
If template-level dictionary access is genuinely useful in several places, add a custom filter:
Save that in a templatetags module, then load it in the template:
This is the standard way to support dynamic dictionary access without trying to force Python syntax into the template engine.
Handle Missing Keys Gracefully
Templates often need a fallback when a key is missing. Combine your output with default:
That prevents blank output from becoming confusing in the UI.
You can also normalize the data in the view:
This is often preferable when the same fallback behavior should be consistent everywhere.
Common Pitfalls
- Trying to write Python indexing syntax directly in the template.
- Assuming dot lookup always works, even for keys that contain spaces, dashes, or dynamic names.
- Pushing too much data-access logic into templates instead of resolving it in the view.
- Forgetting a fallback for missing keys, which can silently render empty output.
- Creating a custom filter when a simpler context value from the view would be clearer.
Summary
- Use dot syntax such as
{{ user_info.name }}for simple dictionary keys. - Do not expect bracket indexing syntax to work in Django templates.
- For dynamic keys, resolve the value in the view or add a custom filter like
get_item. - Use
defaultor view-side normalization to handle missing keys cleanly. - Keep templates presentation-focused and move complex lookup logic back into Python code.

