# Iteration on two dictionaries in my template

**URL:** <https://forum.djangoproject.com/t/iteration-on-two-dictionaries-in-my-template/23084>\
**Category:** Templates & Frontend\
**Created:** [August 15, 2023, 10:35pm UTC](https://forum.djangoproject.com/t/iteration-on-two-dictionaries-in-my-template/23084 "2023-08-15T22:35:28Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hugz59](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/hugz59/32/15385_2.png) [@Hugz59](https://forum.djangoproject.com/u/Hugz59)\
**Post date:** [August 15, 2023, 10:35pm UTC](https://forum.djangoproject.com/t/iteration-on-two-dictionaries-in-my-template/23084/1 "2023-08-15T22:35:28Z")

</div>

Hey !

I don’t know if what i want to do is possible.

I have two dictionnaries :

```auto
registry = {
			'AFNIC':'.fr',
			'VERISIGN': ['.com', '.net', '.name'],
			'DNS Belgium': '.be',
			'Nominet': ['.uk', '.co.uk'],
			'DENIC': '.de',
			'EurID': '.eu',
			'Bakom': '.ch',
			'Neulevel': '.biz',
			'CIRA': '.ca',
			'Afilias': ".info",
			'PIK': '.bzh',
			'Go Daddy': '.cn',
			'Dominios': '.es',
			'Registre IT': '.it',
			'PT': '.pt',
			'Small Registry': ['aeroport.fr', 'avocat.fr', 'chambagri.fr', 'chirurgiens-dentistes.fr', 'experts-comptables.fr', 'experts-comptables.fr', 'medecin.fr', 'geometre-expert.fr', 'notaires.fr', 'pharmacien.fr', 'port.fr', 'veterinaire.fr'],
}

link_registry = {
			'https://www.afnic.fr/' :'AFNIC',
			'https://www.verisign.com/' :'VERISIGN',
			'https://www.dnsbelgium.be/fr' : 'DNS Belgium',
			'https://www.nominet.uk/' : 'Nominet',
			'https://www.denic.de/en/' : 'DENIC',
			'https://eurid.eu/fr/' : 'EurID',
			'https://www.bakom.admin.ch/bakom/fr/home.html' : 'Bakom',
			'' : 'Neulevel',
			'https://www.cira.ca/fr/' : 'CIRA',
			'' : 'Afilias',
			'https://www.pik.bzh/' : 'PIK',
			'https://www.godaddy.com/fr-fr/tlds/nom-de-domaine-cn' : 'Go Daddy',
			'https://www.dominios.es/es' : 'Dominios',
			'https://www.nic.it/it/gestisci-il-tuo-it/dns-check' : 'Registre IT',
			'https://www.pt.pt/pt/' : 'PT',
			'https://www.smallregistry.net/' : 'Small Registry'
}

```

I want to create a table in my template with three columns: domain extension, registry name and registry link.

In my views i get the dictionnaries :

```auto
def dns_registry(request):
    registre_liste = dict_registry.registry
    link = dict_registry.link_registry
    return render(request, 'toolbox/registres.html', {'liste': registre_liste, 'link': link})

```

In my table body i tried different ways :

```auto
                    <tbody>
                        {% for key, values in liste.items %}
                        <tr>
                            <td>{{values}}</td>
                            <td>{{key}}</td>
                        {% endfor %}
                            {% for k, v in link.items %}
                            <td>{{k}}</td>
                            {% endfor %}
                        </tr>
                    </tbody>

```

I think it can’t work.

Do you have an idea without use a database ?

Thx !

---

<div class="post-metadata">

**Author:** ![antoinehumbert](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/antoinehumbert/32/14851_2.png) [@antoinehumbert](https://forum.djangoproject.com/u/antoinehumbert)\
**Post date:** [August 15, 2023, 11:17pm UTC](https://forum.djangoproject.com/t/iteration-on-two-dictionaries-in-my-template/23084/2 "2023-08-15T23:17:19Z")

</div>

The first thing here is that because your _link\_registry_ dictionary maps link to name and _registry_ maps name to domains, you must iterate first on _link\_registry_, then on _registry_ to _chain_ link -\> name -\> domains.

That would give something like the following in your template (which is **wrong** : I will explain below):

```auto
                    <tbody>
                        {% for link, name in link.items %}
                        <tr>
                            <td>{{ liste[name] }}</td>
                            <td>{{ name }}</td>
                            <td>{{ link }}</td>
                        </tr>
                        {% endfor %}
                    </tbody>

```

But, this is **wrong** because the liste[name] syntax is not supported in django templates, and you cannot reference a dict entry using a variable.

So, you have to prepare the data to render in the view:

```auto
def dns_registry(request):
    registre_liste = dict_registry.registry
    link_reg = dict_registry.link_registry
    return render(request, 'toolbox/registres.html', {'registrars': {name: {'link': link, 'domains': registre_liste[name]} for link, name in link_reg.items()}})

```

Then use the following in template

```auto
                    <tbody>
                        {% for name, detail in registrars.items %}
                        <tr>
                            <td>{{ detail.domains }}</td>
                            <td>{{ name }}</td>
                            <td>{{ detail.link }}</td>
                        </tr>
                        {% endfor %}
                    </tbody>

```

Note: instead of defining a dictionary for a registrar details, you could also use a tuple, and use `detail.0` and `detail.1` instead of `detail.link` and `detail.domains`

---

<div class="post-metadata">

**Author:** ![addwebsolution](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/addwebsolution/32/23762_2.png) [@addwebsolution](https://forum.djangoproject.com/u/addwebsolution)\
**Post date:** [August 16, 2023, 5:12am UTC](https://forum.djangoproject.com/t/iteration-on-two-dictionaries-in-my-template/23084/3 "2023-08-16T05:12:11Z")

</div>

Hey, if you want something like this than

 ![image](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/f/f2c61ca143e480d58b61bae48d6863d485d0e6ef.png)

here is what you have to do, the dictionaries `registry` and `link_registry` you are passing. You have iterated them right, now in templates

```auto
{% load general_tags %}

{% for key, val in link_registry.items %}
 {{ key }} ----- {{ val }} -------- {{ registry|get_dictionary_item:val }} <br> <br> <br>
{% endfor %}

```

This `get_dictionary_item` is custom filter which I’ve written in my `general_tags.py` and loaded that within template

```auto
@register.filter
def get_dictionary_item(dictionary, key):
    return dictionary.get(key)

```

---

<div class="post-metadata">

**Author:** ![Hugz59](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/hugz59/32/15385_2.png) [@Hugz59](https://forum.djangoproject.com/u/Hugz59)\
**Post date:** [August 16, 2023, 7:23am UTC](https://forum.djangoproject.com/t/iteration-on-two-dictionaries-in-my-template/23084/4 "2023-08-16T07:23:25Z")

</div>

Thx guys i’ll try after work !

I’ll add a lot of extention and registry.

Do you think it’s better to user a database or i can continue to use my dictionnaries ?

Thx !

---

<div class="post-metadata">

**Author:** ![addwebsolution](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/addwebsolution/32/23762_2.png) [@addwebsolution](https://forum.djangoproject.com/u/addwebsolution)\
**Post date:** [August 16, 2023, 7:53am UTC](https://forum.djangoproject.com/t/iteration-on-two-dictionaries-in-my-template/23084/5 "2023-08-16T07:53:21Z")

</div>

If you have used database to store these values than within templates it would have been a lot easier to access and use it than dictionaries. If your dictionary values are fixed and never going to change than it’s ok to use it but if your data is often changes than every time if there has to be changed anything within those values than you have to change the code and have to deploy again and vice versa. On the other hand if you have used models than you can change it from admin panel and it will get updated without any worries.

---

<div class="post-metadata">

**Author:** ![Hugz59](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/hugz59/32/15385_2.png) [@Hugz59](https://forum.djangoproject.com/u/Hugz59)\
**Post date:** [August 16, 2023, 6:06pm UTC](https://forum.djangoproject.com/t/iteration-on-two-dictionaries-in-my-template/23084/6 "2023-08-16T18:06:20Z")

</div>

I tried your code but it didn’t work.

There is nothing in my table.

Yet I used the same code as you.

```auto
def dns_registry(request):
    registre_liste = dict_registry.registry
    link_reg = dict_registry.link_registry
    return render(request, 'toolbox/registres.html', {'registrars': {name:
    {'link': link, 'domains': registre_liste[name]} for link, name in link_reg.items()}})

```

```auto
                    <tbody>
                        {% for name, detail in registars.items %}
                        <tr>
                            <td> {{detail.domains}} </td>
                            <td>{{name}}</td>
                            <td>{{detail.link}}</td>
                        </tr>
                       {% endfor %}
                    </tbody>

```

---

<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:** [August 16, 2023, 6:24pm UTC](https://forum.djangoproject.com/t/iteration-on-two-dictionaries-in-my-template/23084/7 "2023-08-16T18:24:45Z")

</div>

> [@Hugz59](#):
>
> Do you think it’s better to user a database

In general, I have found it always to be preferable to store data in the database instead of in code - especially since you’re always going to have a database available when running Django.

---

<div class="post-metadata">

**Author:** ![antoinehumbert](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/antoinehumbert/32/14851_2.png) [@antoinehumbert](https://forum.djangoproject.com/u/antoinehumbert)\
**Post date:** [August 16, 2023, 6:52pm UTC](https://forum.djangoproject.com/t/iteration-on-two-dictionaries-in-my-template/23084/8 "2023-08-16T18:52:02Z")

</div>

There was a typo error in the template

registars =\> registrars

---

<div class="post-metadata">

**Author:** ![Hugz59](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/hugz59/32/15385_2.png) [@Hugz59](https://forum.djangoproject.com/u/Hugz59)\
**Post date:** [August 16, 2023, 7:14pm UTC](https://forum.djangoproject.com/t/iteration-on-two-dictionaries-in-my-template/23084/9 "2023-08-16T19:14:38Z")

</div>

It’s working ! You’re a god thx !

---

<div class="post-metadata">

**Author:** ![Hugz59](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/hugz59/32/15385_2.png) [@Hugz59](https://forum.djangoproject.com/u/Hugz59)\
**Post date:** [August 16, 2023, 7:15pm UTC](https://forum.djangoproject.com/t/iteration-on-two-dictionaries-in-my-template/23084/10 "2023-08-16T19:15:12Z")

</div>

I’m not comfortable with databases on django that’s why I went through a dictionary.

But I’ll be interested!
