Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从列表视图中统计发布的数量_Python_Django_Django Views - Fatal编程技术网

Python 从列表视图中统计发布的数量

Python 从列表视图中统计发布的数量,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

我想计算我的列表视图中的“待办事项”数量

视图.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.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 }}