Iteration on two dictionaries in my template

Hey !

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

I have two dictionnaries :

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 :

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 :

                    <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 !

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):

                    <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:

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

                    <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

Hey, if you want something like this than

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

{% 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

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

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 !

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.

I tried your code but it didn’t work.

There is nothing in my table.

Yet I used the same code as you.

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()}})
                    <tbody>
                        {% for name, detail in registars.items %}
                        <tr>
                            <td> {{detail.domains}} </td>
                            <td>{{name}}</td>
                            <td>{{detail.link}}</td>
                        </tr>
                       {% endfor %}
                    </tbody>

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.

There was a typo error in the template

registars => registrars

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

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

But I’ll be interested!