Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.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 在Django中将默认计数注释为0(零)_Python_Django - Fatal编程技术网

Python 在Django中将默认计数注释为0(零)

Python 在Django中将默认计数注释为0(零),python,django,Python,Django,我在views.py中有以下代码 class UserPostListView(ListView): model = Post template_name = 'blog/user_posts.html' context_object_name='posts' paginate_by = 4 def get_queryset(self): user = get_object_or_404(User, username=self.kwarg

我在views.py中有以下代码

class UserPostListView(ListView):
    model = Post
    template_name = 'blog/user_posts.html'
    context_object_name='posts'
    paginate_by = 4

    def get_queryset(self):
        user = get_object_or_404(User, username=self.kwargs.get('username'))
        return Post.objects.filter(author=user).order_by('-date_posted')

    def get_context_data(self, **kwargs):
        context = super(UserPostListView, self).get_context_data(**kwargs)
        user = get_object_or_404(User, username=self.kwargs.get('username'))
        context['postuser'] = Post.objects.filter(author=user).order_by('-date_posted')[:1]
        context['posts'] = Post.objects.filter(author=user).order_by('-date_posted')
        context['postns'] = {d['status__count'] for d in Post.objects.filter(author=user,status="NOT STARTED").order_by('status').values('status').annotate(Count('status'))}
html

{% for post in postns %}
    <div class="col-auto col-ns-st padding-col-st"><span class="vertical-span"><i class="fa fa-circle color-ico-st"></i>&nbsp;{{ post }}</span></div>
{% endfor %}
当我有一篇带有图例“NOT STARTED”的帖子时,代码正在运行并计算帖子的数量,但如果没有帖子,则不会显示任何值

如果没有发布数据,我如何将代码默认为“0”?

由于您根据状态进行筛选,因此在此处使用似乎更有意义:

context['postns']=Post.objects.filter(
作者=用户,状态=“未启动”
).count()
然后,您可以使用以下方法渲染此内容:

<div class="col-auto col-ns-st padding-col-st"><span class="vertical-span"><i class="fa fa-circle color-ico-st"></i>&nbsp;{{ postns }}</span></div>
{{postns}
由于您根据状态进行筛选,因此在此处使用似乎更有意义:

context['postns']=Post.objects.filter(
作者=用户,状态=“未启动”
).count()
然后,您可以使用以下方法渲染此内容:

<div class="col-auto col-ns-st padding-col-st"><span class="vertical-span"><i class="fa fa-circle color-ico-st"></i>&nbsp;{{ postns }}</span></div>

{{postns}
Hi@willem,这给了我一个错误:'Coalesce'对象没有属性'default_alias'@chrislumber:是的,您需要指定注释的名称,因为它不再是像
Count
等那样的“简单”对象。Hi@willem,我尝试过,但仍然没有显示“0”,实际上没有显示div。我只是在问题中写了更多的信息。@chrislumber:那么您可能没有正确地渲染它。@chrislumber:请参阅底部的queryset。既然你过滤了,它就什么都不会过滤。你需要在结尾使用
.count()
。嗨@willem,这给了我一个错误:'Coalesce'对象没有属性'default\u alias'@chrislumber:是的,你需要指定注释的名称,因为它不再是像
count
这样的“简单”对象。嗨@willem,我尝试了,但仍然没有显示“0”,实际上没有显示div。我只是在问题中写了更多的信息。@chrislumber:那么您可能没有正确地渲染它。@chrislumber:请参阅底部的queryset。既然你过滤了,它就什么都不会过滤。最后需要使用
.count()
<div class="col-auto col-ns-st padding-col-st"><span class="vertical-span"><i class="fa fa-circle color-ico-st"></i>&nbsp;{{ postns }}</span></div>