Hi,
# filters.py
-- 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.GET, queryset)
return filter.qs
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
queryset = self.get_queryset()
filter = ProductFilter(self.request.GET, queryset)
context["filter"] = filter
return context
import django_filters
from .models import Items
class ProductFilter(django_filters.FilterSet):
class Meta:
model = Items
fields = ['size', 'category']
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAFecadukpt-gAgFwT5%2BKGM2tp3u3fdFDj98MQHWPgbY42dMGVQ%40mail.gmail.com.
No comments:
Post a Comment