Reputation: 1885
In a django template, I'm accessing objects in a list filtered by the objects' datetime
.
Here's a current image of the object_list
onto an HTML table:
The time part of the datetime
("11:50a.m") must remain regardless of duplicates.
However, the date part of the datetime
("September 9"), must appear only once for each unique date.
Essentially, what I'm looking for would look something like:
How would I go about achieving this effect? I've tried using {% if forloop.first %}
, but I'm unable to find a way to target all "firsts" of unique dates.
This is the current code which corresponds to the first image:
{% for event in object_list %}
<tr>
<td>{{ event.datetime|date:"F j g:ia e" }}: </td>
<td><a href="{% url 'detail' event.id %}">{{ event.venue }}</a></td>
</tr>
{% endfor %}
I've also considered not using the date of the datetime
object and manually coding the dates in the HTML, but wouldn't know how to eventually tie the DOM date to the object_list
according to date.
Upvotes: 0
Views: 56
Reputation: 599788
That is exactly what the ifchanged
tag is for.
I would split your output into two parts, and use ifchanged on the date only.
<tr>
<td>{% ifchanged %}{{ event.datetime|date:"F j" }}{% endifchanged %}</td>
<td>{{ event.datetime|date:"g:ia e" }}: </td>
<td><a href="{% url 'detail' event.id %}">{{ event.venue }}</a></td>
</tr>
Upvotes: 1