[Fixed]-Django 2.0 url parameters in get_queryset


You can obtain the URI positional and named parameters in a class-based view with self.args (a tuple) and self.kwargs (a dictionary) respectively.

Here you defined the category_id as a named parameter, so you can obtain its corresponding value with self.kwargs['category_id']:

class SubcategoriesListView(ListView):
    model = Subcategory
    template_name = 'app/categories/index.html'
    def get_queryset(self):
        return Subcategory.objects.filter(category_id=self.kwargs['category_id'])

Since the id is an integer, you thus filter on category_id, not on category.

Leave a comment