how to iterate through dictionary in a dictionary in django template?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Django templates can iterate nested dictionaries, but the syntax is intentionally limited so that presentation logic stays simple. The important rule is to prepare data in the view and keep the template focused on display. Once the structure is in the context, nested loops with .items are usually enough.
Basic Nested Dictionary Example
Suppose your view passes this context:
In the template, iterate over the outer dictionary first, then the inner dictionary.
This is the standard pattern for nested dictionary display.
Keep Complex Reshaping Out of Templates
Django templates support iteration, but they are deliberately weak at arbitrary data manipulation. If your structure needs filtering, sorting, flattening, or renaming, do it in the view first.
Bad idea:
- complicated nested conditionals in template
- assumptions about missing keys everywhere
- business logic spread across HTML
Better idea:
Then the template becomes even simpler.
Handle Missing or Optional Inner Dictionaries
If inner values might be missing or empty, normalize them in the view so the template does not need defensive logic everywhere.
That keeps the template predictable.
Order and Display Considerations
Dictionaries preserve insertion order in modern Python, but if display order matters, do not rely on accident. Sort or build the structure explicitly in the view.
This is often better than trying to force ordering logic into the template layer.
When a Custom Template Filter Helps
If the nested access pattern repeats across templates, a small custom template filter or inclusion tag can improve readability. But use that only when the logic is truly presentation-related. If the transformation is business logic, keep it in Python view or service code instead.
Example with Table-Like Output
Nested dictionaries are often displayed as grouped tables rather than unordered lists. The same iteration pattern still works:
The structure is simple, and the data preparation still belongs in the view.
Common Pitfalls
- Trying to perform complex data reshaping directly in the template.
- Forgetting to call
.itemson both the outer and inner dictionaries. - Passing inconsistent nested structures and then compensating with template hacks.
- Assuming display order without preparing ordered data in the view.
- Moving business logic into templates just to “avoid writing Python.”
Summary
- Use nested
{% for %}loops with.itemsto iterate dictionaries inside dictionaries. - Prepare and normalize the structure in the view before rendering.
- Keep sorting and transformation logic out of the template whenever possible.
- Use custom tags only for reusable presentation concerns.
- Treat Django templates as a rendering layer, not a data-processing engine.
Related reading
- How to iterate through SparseArray?
- How to iterate through SparseArray?
- How to join two sets in one line without using
- How to JSON serialize sets?
- How to join two generators or other iterables in Python?
- How to keep a Python script output window open?
- How to keep keys/values in same order as declared?
- How to keep track of depth in breadth first search?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.