1👍
✅
There is no need to do filtering here, and definitely not on the Product
model: this is a DetailView
[Django-doc] for the Category
model,s o you can work with:
class CategoryDetailView(DetailView):
model = Category
template_name = 'mainapp/product-category.html'
context_object_name = 'category'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['categories'] = self.model.objects.all()
context['products'] = Product.objects.all()
context['category_products'] = self.object.product_set.all()
return context
This will pass the Category
for the given slug as category
to the template, and the related Product
s as category_products
.
Source:stackexchange.com