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
+91-9920886044

  [image: Linkedin] <https://in.linkedin.com/in/shailesh-yadav-b7ab89110>



On Fri, May 7, 2021 at 7:13 PM Shailesh Yadav <shaileshyadav7...@gmail.com>
wrote:

> What Error are you getting?
>
> Thanks & Regards
> Shailesh Yadav
> +91-9920886044
>
>   [image: Linkedin] <https://in.linkedin.com/in/shailesh-yadav-b7ab89110>
>
>
>
> On Fri, May 7, 2021 at 6:48 PM GWISU MANYANDA <gwisumanya...@gmail.com>
> wrote:
>
>> 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
>> <http://category.name>}}"><h1>{{category.name <http://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;">
>> *{{object.name <http://object.name>}}*
>> <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>
>>
>>
>> --
>> 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+unsubscr...@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
>> <https://groups.google.com/d/msgid/django-users/0943bd72-40aa-459a-819c-ee12d5303957n%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAMQ-AEUMcQ3oLcj5wx8HY5y_o0h75spr_5YgCFpO6cGHx-uT_g%40mail.gmail.com.

Reply via email to