Hi,
I've been trying to introduce a filter system in my Django-ecommerce
website and I've been struggling despite reading the documents, etc. I am
using 'django-filter' and below are filters.py, models.py, views.py and the
template. To check out the project, follow the github link:
https://github.com/First-project-01/Django-ecommerce.

Will be grateful if anyone can sort this out. Thanks in advance.
Regards,
Aritra

#filters.py
class ProductFilter(django_filters.FilterSet):
    price = django_filters.NumberFilter()
    price__gt = django_filters.NumberFilter(field_name='price', lookup_expr=
'gt')
    price__lt = django_filters.NumberFilter(field_name='price', lookup_expr=
'lt')
    class Meta:
        model = Items
        fields = ['size', 'availability']

#views.py
class Product(ListView):
    model = Items
    paginate_by = 6
    template_name = 'products.html'
    def get_context_data(self, **kwargs):
        context = super(Product, self).get_context_data(**kwargs)
        context['filter'] = ProductFilter(self.request.GET, queryset=self.
get_queryset())
        return context
#models.py
AVAILABILITY = (
    ('Y', 'Available'),
    ('N', 'Out of Stock'),
)

SIZES = (
    ('K', 'King - 108 x 120'),
    ('Q', 'Queen - 90 x 108')
)
class Items(BaseModel):
    title = models.CharField(max_length=100, null=True, blank=True)
    price = models.FloatField(null=True, blank=True)
    size = models.CharField(choices=SIZES, default=SIZES[0][0], max_length=1
)
    description = models.TextField(max_length=500)
    availability = models.CharField(choices=AVAILABILITY, default=
AVAILABILITY[0][0], max_length=1)
...

#product-list.html
<div class="col-md-3">
         <form method="get">
            {{ filter.form| crispy }}
            <button type="submit">Search</button>
         </form>
    </div>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFecadtwZG0%3DLwE989gsFkusgAnAGkvEspF3i5WC1-uWO6OOHw%40mail.gmail.com.

Reply via email to