[Answered ]-Django template if type add to html id

2👍

There is nothing wrong with the code you currently have; though, you could make the following small adjustment and get rid of quite a bit of code.

Why not use the gigi.typology string as the id?

{% for gigi in object_list %}
<div id="{{ gigi.typology }}"> 
   <div> {{gigi.name}}</div>
</div>
{% endfor %}

If you are looking at having all the elements for a particular “typology” grouped together then you could also use the regroup clause.

{% regoup object_list by typology as typolist %}
{% for typo in typolist %}
    <div id="{{ typo.grouper }}">
        {% for gigi in typo.list %}
        {{ gigi.name }}
        {% endfor %}
    </div>
{% endfor %}

Leave a comment