#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
# filters.py