Python Django模板如何删除重复值?

Python Django模板如何删除重复值?,python,django,django-templates,Python,Django,Django Templates,有没有一种方法可以告诉Django隐藏/删除(显示空白)与前一行值相同的字段 i、 e:如果now对于不同的文章是相等的,那么它是否只能显示组中的第一篇文章 from django.views.generic.list import ListView from django.utils import timezone from articles.models import Article class ArticleListView(ListView): model = Articl

有没有一种方法可以告诉Django隐藏/删除(显示空白)与前一行值相同的字段

i、 e:如果now对于不同的文章是相等的,那么它是否只能显示组中的第一篇文章

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>
从django.views.generic.list导入ListView
从django.utils导入时区
从articles.models导入文章
类ArticleListView(ListView):
模型=文章
def获取上下文数据(自身,**kwargs):
context=super(ArticleListView,self)。获取上下文数据(**kwargs)
上下文['now']=时区。now()
返回上下文
文章
    {对象_列表%中的项目的百分比}
  • {{article.pub|u date}-{{article.headline}
  • {%empty%}
  • 还没有文章
  • {%endfor%}
文章-现在

a-2017-01-01

b-

c-2017-01-02

d-

这可以从视图或直接在模板中实现吗?

您可以使用:

检查值是否已从循环的最后一次迭代更改

详情如下:

<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>
文章
    {对象_列表%中的项目的百分比}
  • {{article.headline}}-{%ifchanged article.pub_date | date%} {{article.pub|u date}{%endifchanged%}
  • {%empty%}
  • 还没有文章
  • {%endfor%}
这将在每次迭代中检查
article.pub_date
的值,只有当该值发生变化时才会显示该值


祝你好运:)

我不确定那个黄色方块应该显示什么,但你几乎肯定想要。