1

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.

arjun
  • 7,230
  • 4
  • 12
  • 29
saini tor
  • 223
  • 6
  • 21

2 Answers2

4

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.

arjun
  • 7,230
  • 4
  • 12
  • 29
  • perfect answer, but it's displaying `Showing Products 20` and i want something like this `Showing Products 1-12 of 20 Result` – saini tor Jul 23 '20 at 05:57
  • You solve my issues, could you please solve this issue also https://stackoverflow.com/questions/63046835/how-to-filter-product-by-category-wise-in-django – saini tor Jul 23 '20 at 06:06
  • done, how I can chat with you on StackOverflow beside comment section – saini tor Jul 23 '20 at 06:12
  • Hi Arjun, Could you please solve this isue...https://stackoverflow.com/questions/63781362/how-to-display-foreignkey-data-in-django-html-page – saini tor Sep 07 '20 at 16:40
0

You can use Django's count function and then pass it as a part of your view.

Use it like

context = {'count': Product.objects.count()}

And then it should return how many products you have in your queryset. So then, in your HTML file, you can simply render it as

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

EDIT

After reviewing your sample, I believe it should be

Product.objects.filter(subcategory=subcat_slug).count()
crimsonpython24
  • 2,223
  • 2
  • 11
  • 27
  • it's counting the total database products, but I want to count particular category products... – saini tor Jul 23 '20 at 05:04
  • Perhaps `Products.objects.filter(category=subcat_slug).count()`? You can mix the functions together as, after you filter the products, it's still a queryset object. – crimsonpython24 Jul 23 '20 at 05:06
  • I did it but it shows me this error `object of type 'int' has no len()` ` – saini tor Jul 23 '20 at 05:09
  • Can you please tell me how did you use my code? If you tried to parse it as `len(Products.object.filter...)`, remove the `len` as `.count()` will already return the length. – crimsonpython24 Jul 23 '20 at 05:13
  • i used this `productlist = Product.objects.filter(subcategory=subcategory).count()`, please chek in my above view file – saini tor Jul 23 '20 at 05:16
  • If product list is **all objects in a subcategory without the counts**, use `productlist = Product.objects.filter(subcategory=subcategory)` you cannot use `product` because that is **all** objects. You cannot call filter after all. – crimsonpython24 Jul 23 '20 at 05:17
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/218406/discussion-between-saini-tor-and-crimsonpython24). – saini tor Jul 23 '20 at 05:18