# Display Dictionary Values in Template

**URL:** <https://forum.djangoproject.com/t/display-dictionary-values-in-template/26093>\
**Category:** Templates & Frontend\
**Created:** [December 12, 2023, 7:17am UTC](https://forum.djangoproject.com/t/display-dictionary-values-in-template/26093 "2023-12-12T07:17:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jstnice](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jstnice/32/17732_2.png) [@jstnice](https://forum.djangoproject.com/u/jstnice)\
**Post date:** [December 12, 2023, 7:17am UTC](https://forum.djangoproject.com/t/display-dictionary-values-in-template/26093/1 "2023-12-12T07:17:58Z")

</div>

How to display following dictionary in template:  
`inventory = {'1': "{'Name': 'Server1', 'IP': '192.168.1.1', 'Version': 'Win'}", '2': "{'Name': 'Server2', 'IP': '192.168.1.2', 'Version': 'Linux'}", '3': "{'Name': 'Server3', 'IP': '192.168.1.3', 'Version': 'Win'}"}`

I want to display in following format in HTML but I cannot figure it out:

```auto
Hostname IP Version
Server1 192.168.1.1 Win
Server2 192.168.1.2 Linux
Server3 192.168.1.3 Win

```

I tried following method:  
Views.py:  
`return render(request, 'inventory.html', inventory)`

Template:

```auto
    {% for i in inventory.values %}
        {% for Name, IP, Version in i.items %}
        <tr>
        <td>{{ Name }}</td>
        <td>{{ IP }}</td>
        <td>{{ Version }}</td>
        </tr>
        {% endfor %}
   {% endfor %}

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [December 12, 2023, 1:29pm UTC](https://forum.djangoproject.com/t/display-dictionary-values-in-template/26093/2 "2023-12-12T13:29:53Z")

</div>

Side Note: When posting code or templates here, surround the code (or template) between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep the text properly formatted.  
(I’ve taken the liberty of fixing your original post for this.)

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [December 12, 2023, 1:37pm UTC](https://forum.djangoproject.com/t/display-dictionary-values-in-template/26093/3 "2023-12-12T13:37:48Z")

</div>

> [@jstnice](#):
>
> `{% for i in inventory.values %}`

This is a good start.

Your first iteration through this outer loop will set `i` to:

> [@jstnice](#):
>
> “{‘Name’: ‘Server1’, ‘IP’: ‘192.168.1.1’, ‘Version’: ‘Win’}”

Now, this is an issue in that this is a string, and not a dict. Trying to use this in the template is going to be extremely problematic.

So the first thing you’ll want to do is ensure that the values in the `inventory` dict are also of type dict:

`inventory = {‘1’: {‘Name’: ‘Server1’, ‘IP’: ‘192.168.1.1’, ‘Version’: ‘Win’}, ‘2’: {‘Name’: ‘Server2’, ‘IP’: ‘192.168.1.2’, ‘Version’: ‘Linux’}, ‘3’: {‘Name’: ‘Server3’, ‘IP’: ‘192.168.1.3’, ‘Version’: ‘Win’}}

Now that you’ve got the right types, `i` would have the value `{'Name': 'Server1', 'IP': '192.168.1.1', 'Version': 'Win'}`

Because this is a dict, you won’t be iterating over it. You’ll access the values as `i.Name`, etc.

Review the docs at [The Django template language | Django documentation | Django](https://docs.djangoproject.com/en/5.0/ref/templates/language/#variables) for more details.
