[Answer]-Django search form to query only when form is submitted

1👍

You should add get_queryset() method than the doing filtering in get_context_data(). You can add the method as below

def get_queryset(self):
    qs = News.objects.filter(category_id=self.pk)
    #you can change this to just support one of the pub_from or pub_to
    if self.pub_from and self.pub_to :
        qs = qs.filter(published_date__range=(self.pub_from,self.pub_to)
    if self.crawler:
        qs = qs.filter(crawler=self.crawlers)
👤Rohan

Leave a comment