1# blogs/views.py
2
3import operator
4
5from django.db.models import Q
6
7
8class BlogSearchListView(BlogListView):
9 """
10 Display a Blog List page filtered by the search query.
11 """
12 paginate_by = 10
13
14 def get_queryset(self):
15 result = super(BlogSearchListView, self).get_queryset()
16
17 query = self.request.GET.get('q')
18 if query:
19 query_list = query.split()
20 result = result.filter(
21 reduce(operator.and_,
22 (Q(title__icontains=q) for q in query_list)) |
23 reduce(operator.and_,
24 (Q(content__icontains=q) for q in query_list))
25 )
26
27 return result
28