how to sort product by price low to high and high to low in django with fillters

Solution for how to sort product by price low to high and high to low in django with fillters
is Given Below:

i want to sort product list by pricing low to high and high to low and render in template

my models.py

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")
    can_buy = models.ForeignKey(For, on_delete=models.CASCADE, related_name="for_wearing")
    name = models.CharField(max_length=200, blank=False)
    contain_size = models.CharField(max_length=50, blank=True)
    brand_name = models.CharField(max_length=100, blank=False, default="Bagh")
    first = models.ImageField(upload_to='items', blank=False)
    second = models.ImageField(upload_to='items', blank=False)
    third = models.ImageField(upload_to='items', blank=True)
    fourth = models.ImageField(upload_to='items', blank=True)
    fifth = models.ImageField(upload_to='items', blank=True)
    rating = models.FloatField(blank=False,default="4.0")
    item_video = models.FileField(upload_to='item_vedio', blank=True)
    color = models.CharField(max_length=30, blank=False, default="Black")
    material = models.CharField(max_length=50, blank=False, default="Plastic" )
    return_policy = models.CharField(max_length=100, blank=False, default="7Days Return Policy")
    stock = models.CharField(max_length=50, blank=False, default="In Stock")
    price = models.FloatField(blank=False,)
    actual_price = models.FloatField(blank=False)
    type = models.CharField(blank=False, max_length=100, default="washable")
    about = models.CharField(blank=False,max_length=100, default="Lusturous")
    offer = models.CharField(max_length=4, blank=True)
    joined_date = models.DateTimeField(default=timezone.now,editable=False)
    update_at = models.DateTimeField(auto_now=True)
    description = models.TextField(blank=True)
    
    def __str__(self):
        return self.name


class Subcategories(models.Model):
    categories = models.ForeignKey(Categories, on_delete=models.CASCADE, related_name="our_categories")
    name = models.CharField(max_length=200, blank=False)
    joined_date = models.DateTimeField(default=timezone.now,editable=False)
    update_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.name

here is my views.py

class Product(View):
    def get(self, request, subcategory_id):
        subcategory = get_object_or_404(Subcategories, pk=subcategory_id)
        products = subcategory.products.all()
        category_list = Categories.objects.all()
        print(products)
        return render (request, 'products.html',{"subcategory_list" : products, 'category_list': category_list })

my html

<div class="col-10">
            <ul>
              <li class="filters"><strong>Sort By :</strong></li>
              <li class="filters"><a class="filter_by" href="">Popularity</a></li>
              <li class="filters"><a class="filter_by" href="">Price:--low to high</a></li>
              <li class="filters"><a class="filter_by" href="">Price:-- high to low</a></li>
              <li class="filters"><a class="filter_by" href="">Newest First</a></li>
              <li class="filters"><a class="filter_by" href="">Discount</a></li>
            </ul>
        </div>
        <section class="col-10">
          {% for products in subcategory_list %}
          <a class="products" href="{% url 'products:detail' item_id=products.id %}">
          <div class="card col-4" style="width: 18rem; height:420px">
              <img class="img" src="{{ products.first.url }}" alt="" height="300px" width="100%" onmouseover="this.src="https://codeutlity.org/questions/68604759/{{ products.second.url }}""
              onmouseout="this.src="{{ products.first.url }}"" >
              <div class="detail">
              <h4 class="price">{{ products.price|currency}}</h4>
              <h5>{{products.name}} <br> <span class="brand">{{ products.brand_name}}</span> <span class="btn btn-success buton">✔Authentic</span> </h5>
              <p class="rate">Rate: {{products.rating}}<i class="fas fa-star"></i></p>
              <h6>Size:<span> {{products.contain_size}}</span> </h6>
              </div>
            </div>
          </a>
            {% endfor %} 
          </div>  
        </section>

as you can see i created a div and in that i put sorting so what i want when a user click on that link it render the product list in that order but right i have no idea how to that i saw some document but that doesn’t seem the best way to achieve that any idea which is the best way to achieve that
thank you for your time

In your html

<li class="filters"><a class="filter_by" href="?sort=l2h">Price:--low to high</a></li>
<li class="filters"><a class="filter_by" href="?sort=h2l">Price:-- high to low</a></li>

In your view

def get(self, request, subcategory_id):
        subcategory = get_object_or_404(Subcategories, pk=subcategory_id)

        sort_by = request.GET.get("sort", "l2h") 
        if sort_by == "l2h":
           products = subcategory.products.order_by("price")
        elif sort_by == "h2l":
           products = subcategory.products.order_by("-price")

        category_list = Categories.objects.all()
        return render (request, 'products.html',{"subcategory_list" : products, 'category_list': category_list })

Explanation :

When you click on the sorting link from frontend, just look out at your url in browser.
We are sending a sort parameter which tells how should we sort it.

We retrieve that parameter in our view

sort_by = request.GET.get("sort", "l2h") 

Note : Here if no parameter is provided ( When user visits that page ) it will be sorted by l2h aka low to high

You can change that accordingly, what default sorting/filtering you want

Finally, we are just using django query to filter out our data.

And use that in your template