How to List Items In A Django Dictionary in Templates

By Rich Jones

Here's a quick to tutorial on how to iterate over a dictionary in a Django template. I get asked this quite a lot so I figured I'd put it online in case anybody is Googling it.

Dictionary Items

First, in your view, make sure that you're returning a rendered response that looks something like this:

Then, in your template.html, do something like this:

And that's it! The key/value pairs in your dictionary will be rendered in HTML as an unordered list.

Hang ups

The common mistake here is to put 'for key, value in dictionary' rather than 'for key, value in dictionary.items' - it's a very simple mistake to make, and an easy one to forget.

 

 

Best Sites to Find Clients as a Freelance Developer

Fill your pipeline with best-fit clients for your rate, engagement length, and role preferences.

Take The Quiz

 


See More Posts About:

Django


Posted by Rich Jones

LinkedIn Website