Kindly help me with Django filer, urgent need.

13 views
Skip to first unread message

Aritra Ray

unread,
Jun 25, 2021, 9:04:37 AM6/25/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']

Symaxx

unread,
Jun 25, 2021, 10:04:25 AM6/25/21
to django...@googlegroups.com
This is easier to work with on GitHub, do you have a GitHub account?

You can raise an issue then share the link here 🙂

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAFecadt5eYOdDX3sL46kbscLfbhwAzEcvwUpM9TF_s%2B9YhBGVQ%40mail.gmail.com.

Aritra Ray

unread,
Jun 25, 2021, 12:57:00 PM6/25/21
to django...@googlegroups.com
Right, kindly heed to the problem as early as possible, The GitHub link is provided below. 

Reply all
Reply to author
Forward
0 new messages