title: how to do sorting of a search result item in django
body: <p>i have search field i want to give user sorting option so user can do sorting for price low to high to low in the search result but unable to achieve that
  my view</p>
  <pre><code>class search_item(View):
      def get(self, request):
          search_item = request.GET.get('search')
          if search_item:
              items = Item.objects.filter(Q(name__icontains=search_item)|Q(color__icontains=search_item))
              sort_by = request.GET.get(&quot;sort&quot;, &quot;l2h&quot;) 
              if sort_by == &quot;l2h&quot;:
                  item = items.order_by(&quot;price&quot;)
              elif sort_by == &quot;h2l&quot;:
                  item = items.order_by(&quot;-price&quot;)
              return render(request, 'search_result.html', {'items':item})
  </code></pre>
  <p>it does the search but unable to do sorting what wrong in my code</p>
  <p>html</p>
  <pre><code> &lt;li&gt;&lt;a class=&quot;dropdown-item&quot; href=&quot;?sort=l2h&quot;&gt;Low To High&lt;/a&gt;&lt;/li&gt;
   &lt;li&gt;&lt;a class=&quot;dropdown-item&quot; href=&quot;?sort=h2l&quot;&gt;High To Low&lt;/a&gt;&lt;/li&gt;
  </code></pre>
  <p>my model</p>
  <pre><code>class Item(models.Model):
      categories = models.ForeignKey(Categories, on_delete=models.CASCADE, related_name='our_items')
      subcategories = models.ForeignKey(Subcategories, on_delete=models.CASCADE, related_name='products')
      name = models.CharField(max_length=200, blank=False)
      swag = models.BooleanField(blank=False, default=False)
      male = models.BooleanField(blank=False, default=False)
      female = models.BooleanField(blank=False, default=False)
      unisex = models.BooleanField(blank=False, default=False)
      first = models.ImageField(upload_to='items', blank=False)
      second = models.ImageField(upload_to='items', blank=False)
      third = models.ImageField(upload_to='items', blank=True)
      color = models.CharField(max_length=30, blank=False, default='Black')
      material = models.CharField(max_length=50, blank=False, default='Plastic' )
      price = models.FloatField(blank=False,)
      description = models.TextField(blank=True)
      
      @staticmethod
      def get_items_by_id(ids):
          return Item.objects.filter(id__in = ids)
  
      def __str__(self):
          return self.name
  </code></pre>
  <p>my url</p>
  <pre><code>path('search-result/',search_item.as_view(),name='search')
  </code></pre>
  <p>error when i to do sorting by reuslted item price
  The view products.views.search_item didn't return an HttpResponse object. It returned None instead.</p>
  <p>traceback</p>
  <pre><code>Traceback (most recent call last):
    File &quot;C:\Users\mithlesh\Desktop\coolbuy2.0\lib\site-packages\django\core\handlers\exception.py&quot;, line 47, in inner
      response = get_response(request)
    File &quot;C:\Users\mithlesh\Desktop\coolbuy2.0\lib\site-packages\django\core\handlers\base.py&quot;, line 188, in _get_response
      self.check_response(response, callback)
    File &quot;C:\Users\mithlesh\Desktop\coolbuy2.0\lib\site-packages\django\core\handlers\base.py&quot;, line 309, in check_response
      raise ValueError(
  
  Exception Type: ValueError at /search-result/
  Exception Value: The view products.views.search_item didn't return an HttpResponse object. It returned None instead.
  </code></pre>
  <p>view in which sorting work perfectly fine</p>
  <pre><code>class Product(View):
      def get(self, request, subcategory_id):
          subcategory = get_object_or_404(Subcategories, pk=subcategory_id)
  
          sort_by = request.GET.get(&quot;sort&quot;, &quot;l2h&quot;) 
          if sort_by == &quot;l2h&quot;:
             products = subcategory.products.order_by(&quot;price&quot;)
          elif sort_by == &quot;h2l&quot;:
             products = subcategory.products.order_by(&quot;-price&quot;)
          sort_by = request.GET.get(&quot;sort&quot;, &quot;newest&quot;)
          if sort_by == &quot;unisex&quot;:
              products = subcategory.products.order_by(&quot;-unisex&quot;)
          elif sort_by == &quot;newest&quot;:
              products = subcategory.products.order_by(&quot;-update_at&quot;)
          sort_by = request.GET.get(&quot;sort&quot;, &quot;male&quot;)
          if sort_by == &quot;male&quot;:
              products = subcategory.products.order_by(&quot;-male&quot;)
          elif sort_by == &quot;female&quot;:
              products = subcategory.products.order_by(&quot;-female&quot;)
  
          category_list = Categories.objects.all()
          return render (request, 'products.html',{&quot;subcategory_list&quot; : products, 'category_list': category_list })
  </code></pre>