【发布时间】:2021-04-11 23:40:54
【问题描述】:
我正在拼命地尝试计算模型中某个类别中的对象。更清楚地说:我正在抓取新闻文章并且我有这个模型:
class News(models.Model):
title = models.CharField(max_length=2000)
link = models.CharField(max_length=2083, default="", unique=True)
published = models.DateTimeField()
desc = models.CharField(max_length=2083)
site = models.CharField(max_length=30, default="", blank=True, null=True)
这个视图过滤对象并根据请求提供服务:
class SearchResultsView(ListView):
model = News
template_name = 'search_results.html'
context_object_name = 'articles'
def get_queryset(self):
query = self.request.GET.get('q')
min_dt = self.request.GET.get('qf')
max_dt = self.request.GET.get('qt')
object_list = News.objects.filter(Q (title__icontains=query)|
Q (desc__icontains=query) & Q (published__range=(min_dt, max_dt))).order_by('-published')
我需要在我的 html django 网站上为每个网站提供多少篇文章
这是我的html
<div class="row">
<table>
<tbody>
<h2>{{ articles.count }} articles found in total </h2>
HERE I NEED TO PUT HOW MANY ARTICLES PER SITE BELOW IS
THE ACTUAL LIST ON A TABLE
{% for a in articles %}
<tr>
<td>
<a href="{{ a.link }}">{{ a.title }}</a>
</td>
<td>
<p>
Source: {{ a.site }}
</p>
</td>
<td>
<p>
Published: {{ a.published }}
</p>
</td>
<td>
<p>
More info: {{ a.desc }}
</p>
</td>
</tr>
{% endfor %}
</tbody>
</table>
换句话说,我需要按站点分组并计算结果。
谢谢!
【问题讨论】:
标签: django templates filter count