[Django]-Filter Option with class based view django

4👍

You don’t get the URL parameter with self.request.GET, but with self.kwargs, so:

class TopicView(generic.ListView):
    template_name = 'blog/posts.html'
    context_object_name = 'posts'

    def get_queryset(self):
        return Post.objects.filter(
            topic__contains=self.kwargs['topic']
        ).order_by('-pub_date')

Leave a comment