I have a table Events
, ordered by a field date
.
I want to print out the events in the template, but using a separate div for each date, e.g.:
<div class="content">
<h1>December 30th</h1>
<!-- div for Event 1 from December 30th -->
<!-- div for Event 2 from December 30th -->
</div>
<div class="content">
<h1>December 31st</h1>
<!-- div for Event 1 from December 31st -->
<!-- div for Event 2 from December 31st -->
<!-- div for Event 3 from December 31st -->
</div>
How do I do this?
My current solution is to put the Event objects in a dictionary with the date as the key. This has ordering issues and is inelegant and inefficient.
View:
events = Event.objects.select_related().all()
events_dict={}
for event in events:
date=event.date.strftime('%d %B %Y')
if date in events_dict:
events_dict[date].append(event)
else:
events_dict[date] = [event]
Template:
{% for date, events in events_dict.items %}
<div class="content">
<h1>{{date}}</h1>
{% for event in events %}
{% include "partials/event.html" %}
{% endfor %}
</div>
{% endfor %}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…