Reputation: 2784
Is there a way to say to Django to hide/remove (show a blank space) for fields that got same values as previous row?
i.e.: if now is equal for differents Articles can it be show only for the first of the group?
from django.views.generic.list import ListView
from django.utils import timezone
from articles.models import Article
class ArticleListView(ListView):
model = Article
def get_context_data(self, **kwargs):
context = super(ArticleListView, self).get_context_data(**kwargs)
context['now'] = timezone.now()
return context
<h1>Articles</h1>
<ul>
{% for article in object_list %}
<li>{{ article.pub_date|date }} - {{ article.headline }}</li>
{% empty %}
<li>No articles yet.</li>
{% endfor %}
</ul>
Article - now
a - 2017-01-01
b -
c - 2017-01-02
d -
Is this possible from view or directly in template?
Upvotes: 1
Views: 2953
Reputation: 23144
You can use ifchanged which:
Checks if a value has changed from the last iteration of a loop.
as follows:
<h1>Articles</h1>
<ul>
{% for article in object_list %}
<li>{{ article.headline }} - {% ifchanged article.pub_date|date %}
{{ article.pub_date|date }} {% endifchanged %}
</li>
{% empty %}
<li>No articles yet.</li>
{% endfor %}
</ul>
This will check in each iteration the value of article.pub_date
and only when that value changes it will be displayed.
Good luck :)
Upvotes: 3