title: How to apply pagination to a filtered queryset in Django
body: <p>I'm currently stuck with applying pagination to my Django view codes. Basically, paginate_by seems automatically to work with <code>queryset</code> attribute in CBV. But, in my case, I'm not just using <code>queryset</code> attribute. I'm using filtered <code>queryset</code> on my own. How can I apply pagination to my codes?</p>
  
  <pre><code>class SearchListView(ListView):
      model = Store
      template_name = 'boutique/search.html'
      paginate_by = 5
  
      def get(self, request, *args, **kwargs):
  
            search_text = request.GET.get('search_text')
            sorter = request.GET.get('sorter')
  
            if not sorter:
                sorter = 'popularity'
  
            if search_text:
                search_stores = Store.objects.filter(Q(businessName__icontains=search_text) | Q(mKey__icontains=search_text))
                if sorter == 'businessName':
                    search_stores = search_stores.order_by(sorter)
                else:
                    search_stores = search_stores.order_by(sorter).reverse()
            else:
                search_stores = ''
  
            for store in search_stores:
                store.mKey = store.mKey.split(' ')
  
            return render(request, self.template_name, {
                'search_stores': search_stores,
                'search_text': search_text,
                'sorter': sorter,
            })
  </code></pre>
  
  <p><strong>-------------------------UPDATE2-------------------------</strong></p>
  
  <p>I'm stuck with getting inputs from HTML input tags. Currently, I just put <code>self.kwargs()</code>, but I don't know what I should use instead of them.</p>
  
  <pre><code>class SearchListView(ListView):
      model = Store
      template_name = 'boutique/search.html'
      paginate_by = 5
  
      def get_context_data(self, **kwargs):
          context = super().get_context_data(**kwargs)
          context['search_text'] = self.kwargs['search_text']
          context['sorter'] = self.kwargs['sorter']
          return context
  
      def get_queryset(self):
  
          search_text = self.kwargs['search_text']
          sorter = self.kwargs['sorter']
  
          if not sorter:
              sorter = 'popularity'
  
          if search_text:
              search_stores = Store.objects.filter(Q(businessName__icontains=search_text) | Q(mKey__icontains=search_text))
              if sorter == 'businessName':
                  search_stores = search_stores.order_by(sorter)
              else:
                  search_stores = search_stores.order_by(sorter).reverse()
          else:
              search_stores = ''
  
          for store in search_stores:
              store.mKey = store.mKey.split(' ')
  
          return search_stores
  </code></pre>