In need of urgent help as I've been stuck on this for a while

14 views
Skip to first unread message

Aritra Ray

unread,
Jun 24, 2021, 9:47:25 AM6/24/21
to django...@googlegroups.com
Hi,
This is my second request regarding the same issue. I've been building a Django E-commerce website and I'm facing problems filtering the product according to price. I used django-filter to filter category and size. Please suggest a solution for price in the following manner: Latest products, Lowest to Highest and Highest to Lowest. I've made some progress in the template without using django-filter. Kindly help me tie it up with the backend.

Do let me know if anything else is needed. 
Thanks for your help in advance

Regards,
Aritra

#products-html
 <!--Filter-->
    <div class="row">
      <div class="container">
          <form method="get"style="width:20%">
            {{filter.form|crispy}}
            <div class="row">
              <label>Select sorting criteria</label>
              <div class="control">
                  <div class="select">
                      <select name="sorting">
                          <option value="-date_added"{% if sorting == '-date_added' %}selected{% endif %}>Latest collection</option>
                          <option value="price"{% if sorting == 'price' %} selected {% endif %}>Lowest to Highest</option>
                          <option value="-price"{% if sorting == '-price' %} selected {% endif %}>Highest to Lowest</option>
                      </select>
                  </div>
              </div>
          </div>
            <button type="submit" class="btn btn-warning btn-sm"><span class="glyphicon glyphicon-search"></span> Search</button>
        </form> 
      </div>
    </div>   

#views.py

class Product(ListView):
    model = Items
    paginate_by = 6
    template_name = 'products.html'
    ordering = ["-id"]

    def get_queryset(self):
        queryset = super().get_queryset()
        filter = ProductFilter(self.request.GETqueryset)
        return filter.qs

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        queryset = self.get_queryset()
        filter = ProductFilter(self.request.GETqueryset)
        context["filter"= filter
        return context

# filters.py
import django_filters
from .models import Items

class ProductFilter(django_filters.FilterSet):
    class Meta:
        model = Items
        fields = ['size''category']
  
Reply all
Reply to author
Forward
0 new messages