Categories
Python Answers

How to access a dictionary element in a Python Django template?

Spread the love

To access a dictionary element in a Python Django template, we can use the items property.

For instance, if we have

choices = {'key1':'val1', 'key2':'val2'}

Then we can loop through the choices entries with

<ul>
{% for key, value in choices.items %} 
  <li>{{key}} - {{value}}</li>
{% endfor %}
</ul>

We get the key and value pairs from choices.items and then we render them in the li element.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *