26π
β
Youβre looking for the {% regroup %}
tag, which does exactly what you want. It takes a sequence of items (it has to be ordered beforehand, which yours is) and a lookup and groups the sequence by that lookup.
view:
events = Event.objects.select_related.all()
template:
{% regroup events by date as events_by_date %}
{% for date in events_by_date %}
<div class="content">
<h1>{{ date.grouper|date:"d F Y" }}</h1>
{% for event in date.list %}
{% include "partials/event.html" %}
{% endfor %}
</div>
{% endfor %}
(Notice that the date format string is different; the equivalent of %B
in strftime
is F
for the date
filter).
π€Ismail Badawi
Source:stackexchange.com