Python 从列表视图中统计发布的数量
我想计算我的列表视图中的“待办事项”数量 视图.pyPython 从列表视图中统计发布的数量,python,django,django-views,Python,Django,Django Views,我想计算我的列表视图中的“待办事项”数量 视图.py class DashboardListView(LoginRequiredMixin,ListView): model = Links template_name = 'dashboard/home.html' context_object_name ='links_list' paginate_by = 15 def get_queryset(self): return self.m
class DashboardListView(LoginRequiredMixin,ListView):
model = Links
template_name = 'dashboard/home.html'
context_object_name ='links_list'
paginate_by = 15
def get_queryset(self):
return self.model.objects.filter(author=self.request.user)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['dashboard_list']= Dashboard.objects.filter(author=self.request.user)[:15]
context['todo_list']= Todo.objects.filter(author=self.request.user).order_by('-pk')[:15]
context['PasswordUsername_list']= PasswordUsername.objects.filter(author=self.request.user)
return context
并在我的模板中使用{c_count}}呈现它,但无法这样做
谢谢因为您可能也会渲染列表,所以使用可能是执行此操作的最快方法,因为它将获取对象并计算长度,因此我们可以像这样渲染:
{{ todo_list|length }}
谢谢,我只想为完整的todo呈现数字,我在models.py中已经有一个boelian,我该怎么做呢?@GaëtanGR:那么你应该在
查询集上做一个适当的.filter(…)
,类似于todo.objects.filter(status='done')
,当然这取决于模型本身。
<!-- only interested in the count, not in the objects -->
{{ todo_list.count }}