List Products of one Category

530 views
Skip to first unread message

GWISU MANYANDA

unread,
May 7, 2021, 9:19:42 AM5/7/21
to Django users
Hi Every one! I 'm looking for help.
I'm working with Ecommerce  I have a few categories and I would like to list the products of one Category not List of products of each Categories

My model
class Category(models.Model):
name = models.CharField(max_length=200)
def __str__(self):
return self.name


class Item(models.Model):
title = models.CharField(max_length=200)
price = models.FloatField()
discount_price = models.FloatField(blank=True, null=True)
category = models.ForeignKey(Category, on_delete=models.CASCADE, default=False, null=True)
label = models.CharField(choices=LABELS_CHOICES, max_length=1)
slug = models.SlugField()
description = models.TextField()
image = models.ImageField(upload_to = 'static/images')


def __str__(self):
return self.title

My View

def allproduct(request):
category = request.GET.get('category')
if category == None:
     allproduct = Item.objects.all()
else:
       allproduct = Item.objects.filter(category__name=category)

categories = Category.objects.all()
context = {
'categories':categories,
'allproduct':allproduct
}
return render(request,'allproduct.html',context)

My Template
<div class="col-md-6 mb-4" style="margin-top: 10rem !important;">
<h1><strong>All Products </strong></h1>
{% for category in categories %}
<li class="list-group-item">
<a href="{% url 'allproduct' %}?category={{category.name}}"><h1>{{category.name}}</h1></a>
</li>
<div
class="row fadeIn">

{% for object in category.item_set.all %}

<div class="col-md-6 mb-4" style="max-width: 90%;">
<div class="card" style="padding: 20px; width: 450px; height: 450px;">
<div class="container" >
<img src="{{object.image.url}}" class="card-img-top" style="width: 300px; height: 300px;"
alt="">
<a>
<div class="mask rga-white-slight"></div>
</a>
</div>
<div class="card-body text-center">
<p class="lead">
{% if object.discount_price %}
<span class="mr-1">
<del>${{ object.price }}</del>
</span>
<span>${{ object.discount_price }}</span>
{% else %}
<span>${{ object.price }}</span>
{% endif %}
</p>

</h4>
</div>
</div>
</div>
{% endfor %}
</div>
{% endfor %}
</div>


Shailesh Yadav

unread,
May 7, 2021, 9:45:02 AM5/7/21
to django...@googlegroups.com
What Error are you getting?

Thanks & Regards
Shailesh Yadav

   Linkedin 




--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0943bd72-40aa-459a-819c-ee12d5303957n%40googlegroups.com.

Shailesh Yadav

unread,
May 7, 2021, 9:47:45 AM5/7/21
to django...@googlegroups.com
In views use like this.

def allproduct(request):
allproduct = None
allcategorie = Category.objects.all();
categoryID = request.GET.get('category')
print('Category ID is : ', categoryID) ##check if it is showing correct
if categoryID:
allproduct = Product.objects.filter(category=categoryID)
else:
allproduct = Product.objects.all();
data = {} Remaing code-------

Thanks & Regards
Shailesh Yadav

   Linkedin 



GWISU MANYANDA

unread,
May 7, 2021, 4:08:28 PM5/7/21
to django...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages