samedi 3 juillet 2021

How to count number of product within category in django

Actually i want to count number of coupons ( product ) within stores (category) and show them in stores nothing but index.html page.

Here is my models.py

class stores(models.Model): 
  stores_name = models.CharField(max_length=50)
  slug = models.CharField(max_length=25, default=randomString , unique = True)
  .....(some stores info )

  def get_absolute_url(self):
     return reverse('Stores:couponstores', args=[str(self.slug)])

class coupon(models.Model):
  cfid = models.ForeignKey(stores, on_delete=models.CASCADE)
  cname = models.CharField(max_length = 100)
  ....(some coupons info )

  def get_absolute_url(self):
    return reverse('Stores:coupon',args=[self.id,])

Views.py

def index(request):
  stores = stores_model.objects.all()
  context = {'stores': stores}
  return render(request,'Stores/index.html', context)


def coupons(request,stores_slug=None):
   St = None
   allstores = stores_model.objects.all()
   allcoupons = coupon_model.objects.all()
   if stores_slug: 
      St = get_object_or_404(stores_model , slug=stores_slug )
      allcoupons = allcoupons.filter(cfid = St)
   return render(request,'Stores/coupons.html', {'allcoupons':allcoupons,'allstores':allstores,'St':St,})

def coupon_details(request,id):
  allcoupons = get_object_or_404(coupon_model , id=id)
  return render(request,'Stores/coupons.html',{'allcoupons':allcoupons})

Here my templates index.html


coupons.html ......

Thanks in advanced




Aucun commentaire:

Enregistrer un commentaire