Reputation: 319
I have a list of brands. Each brand has a list of categories I want to make each brand a link. And when the user click the brand he passes to the page with all the categories related to that brand
Here is a template with brands:
<ul>
{% for brand in object_list %}
<li><a href="{{ brand.get_absolute_url pk }}">{{ brand.name }}</a></li>
{% endfor %}
</ul>
And here is the view:
class CategoryListView(ListView):
model = Category
queryset = Category.objects.filter(brand=pk)
template_name = 'category_list.html'
But it gives an error. Can someone help me? Thank you
Upvotes: 3
Views: 3559
Reputation: 52018
Override get_queryset
method:
class CategoryListView(ListView):
model = Category
template_name = 'category_list.html'
def get_queryset(self, **kwargs):
qs = super().get_queryset(**kwargs)
return qs.filter(brand_id=self.kwargs['pk'])
And the url should look like this:
path('category/<int:pk>/', CategoryListView.as_view())
Upvotes: 6