I have created three models Main Category , Category and Product Category model contains the Foreign Key of main category and product model contains the Foreign Key of main category and category model Product model contains the product details.
models.py
class BaseModel(models.Model):
id = models.UUIDField(primary_key=True, unique=True,
editable=False, default=uuid.uuid4)
created_at = models.DateTimeField(auto_now_add=True)
class Meta:
abstract = True
class MainCategory(BaseModel):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class Category(BaseModel):
main_category = models.ForeignKey(
MainCategory, on_delete=models.CASCADE, null=True, blank=True)
name = models.CharField(max_length=100)
def __str__(self):
return self.main_category.name + "---" + self.name
class Product(BaseModel):
name = models.CharField(max_length=100)
description = models.TextField()
main_category = models.ForeignKey(
MainCategory, on_delete=models.CASCADE, related_name='main_category')
category = models.ForeignKey(
Category, on_delete=models.CASCADE, related_name='category', null=True,
blank=True)
slug = models.SlugField(unique=True, max_length=100, null=True, blank=True)
def save(self, *args, **kwargs):
self.slug = generate_slug(self.name)
super(Product, self).save(*args, **kwargs)
Views.py
def categories(request):
main_categories = MainCategory.objects.all()
context = {
'main_categories': main_categories
}
return render(request, 'allProducts.html', context)
def showProductsMainCategoryWise(request):
main_category = request.GET.get('main_category')
if main_category == None:
products = Product.objects.order_by('id')
else:
products = Product.objects.filter(main_category__name=main_category)
main_categories = MainCategory.objects.all()
context = {
'products': products,
'main_categories': main_categories
}
return render(request, 'allProducts.html', context)
def showProductsCategoryWise(request):
main_category = request.GET.get('main_category')
category = request.GET.get('category')
if main_category == None and category == None:
products = Product.objects.order_by('id')
else:
products = Product.objects.all().filter(
Q(main_category__name=main_category), Q(category__name=category))
main_categories = MainCategory.objects.all()
categories = Category.objects.all()
context = {
'categories': categories,
'products': products,
'main_categories': main_categories
}
return render(request, 'allProducts.html', context)
product.html
{% extends 'base.html' %}
{% block body %}
<div class="container my-5">
<div class="row">
<div class="col-sm-4">
<div class="card mx-2" style="width: 18rem;">
<div class="card-header text-center">
<a href="{% url 'showProductsMainCategoryWise' %}">All Product</a>
</div>
<ul id="myUL">
{% for i in main_categories %}
<li>
<span class="caret">
<a href="{% url 'showProductsMainCategoryWise' %}?main_category={{i.name}}">{{i.name}}</a>
</span>
<ul class="nested">
{% for cat in i.category_set.all %}
<a
href="{% url 'showProductsCategoryWise' %}?main_category={{cat.main_category.name}} and category={{cat.name}}">
<li>{{cat.name}}</li>
</a>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
</div>
</div>
<div class="col-sm-8">
<table class="table">
<thead>
<tr>
<th scope="col">S/N</th>
<th scope="col">Produc Name</th>
</tr>
</thead>
{% if products %}
<tbody>
{% for p in products %}
<tr>
<th scope="row">{{forloop.counter}}</th>
<td>
<a href="{{p.slug}}">
{{p.name}}
</a>
</td>
</tr>
{% endfor %}
</tbody>
{% else %}
<p>Comming Soon...</p>
{% endif %}
</table>
</div>
</div>
</div>
{% endblock body %}
According to the image above
If you click on all product then all the products are showing and if you click on women fashion, men’s fashion TV, mobiles then they are showing the products according to that main category but when you click on women clothing nothing is showing
So please tell me if showProductsCategoryWise method and product.html file is correct or where something is wrong or something else needs to be added.