[Answered ]-Django template print only first unique occurance

0👍

You can use {% ifchanged %} template tag:

{% for post in posts %}
    {% ifchanged %}<h3>{{ post.date }}</h3>{% endifchanged %}
    <div>{{ post.name }}</div>
{% endfor %}

2👍

There is an inbuilt template tag for that – regroup!

This isn’t perfect, but with the documentation it should get you close.

{% regroup posts by date as date_list %}

<ul>
{% for date in date_list %}
    <li>{{ date.grouper }}
    <ul>
        {% for item in date.list %}
          <li>{{ date.name }}</li>
        {% endfor %}
    </ul>
    </li>
{% endfor %}
</ul>

Leave a comment