Search code examples
pythondjangodjango-viewsdjango-templatesdjango-pagination

How to count products in Django template?


I am getting products according to the category, and I want to display product after the count in this format Showing Products 1-24 of 10 Result, my products are displaying on a category basis, please let me know how I can display the product after the count in that category. it meand if a category have 20 products then it will display 20 product n my product list page.

class SubCategory(models.Model):
    subcat_name=models.CharField(max_length=225)
    subcat_slug=models.SlugField(max_length=225, unique=True)
    category = models.ForeignKey('Category', related_name='subcategoryies', on_delete=models.CASCADE, blank=True, null=True)

and here is my product models.py file...

class Product(models.Model):
    name=models.CharField(max_length=225)
    slug=models.SlugField(max_length=225, unique=True)
    subcategory=models.ForeignKey('SubCategory', related_name='prosubcat', on_delete=models.CASCADE, blank=True, null=True)

    def __str__(self):
        return self.name

here is my model.py file code...

def SubCategorySlugListAPIView(request, subcat_slug):
    subcategories = SubCategory.objects.all()
    product = Product.objects.all()
    if subcat_slug:
        subcategory = get_object_or_404(SubCategory, subcat_slug=subcat_slug)
        productlist = product.filter(subcategory=subcategory)
        paginator = Paginator(productlist, 12)
        page_number = request.GET.get('page')
        product = paginator.get_page(page_number)
template_name = 'mainpage/cat-products.html'
context = {'product': product,
           'subcategories': subcategories, 'subcategory': subcategory}
return render(request, template_name, context)

and here is my cat-products.html file...

<div class="search-count">
    <h5>Showing Products  {{product.count}}</h5>
</div>

nothing displaying here, plese let me know how i can display the product count here.


Solution

  • Try changing your view like this.

       def SubCategorySlugListAPIView(request, subcat_slug):
            subcategories = SubCategory.objects.all()
            subcategory = get_object_or_404(SubCategory, subcat_slug=subcat_slug)            
            productlist = Product.objects.filter(subcategory=subcategory)
            paginator = Paginator(productlist, 12)
            page_number = request.GET.get('page')
            product = paginator.get_page(page_number)
            template_name = 'mainpage/cat-products.html'
            context = {'product': product,
                   'subcategories': subcategories, 'subcategory': subcategory}
            return render(request, template_name, context)

    OR you can also do it in the template with this.

    <h5>Showing Products  {{subcategory.prosubcat.all.count}}</h5>
    

    EDIT: Here I can see you are using paginator so you can do it with this.

    Showing products {{product.start_index}} - {{prodct.end_index}} of {{product.count}} results.
    

    See the docs for more details on pagination.