嗨,我需要帮助,我想在父母类别中显示总数,我该怎么办?

问题描述 投票:0回答:1
def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    context['category_list']=Category.objects.filter(parent__isnull=True).annotate(num=Count('products'))

    return context

index.html

   {% for category in category_list %}
    <div class="single-category">
        <a href="{{category.get_absolute_url}}">
            {{category.title}} 
            {{category.num}}
            <span style="color: gray;"> 
            </span>
        </a>
    </div>
    {% endfor%}

我有这样的数据电子0职位0但在儿童类别中,我有两个职位,我想在所有子类别的父类别中显示总帖子数

django python-3.x django-models django-views jinja2
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.