Need urgent help with filter system (django-filter) in Django

40 views
Skip to first unread message

Aritra Ray

unread,
Jun 23, 2021, 8:55:50 AM6/23/21
to django...@googlegroups.com
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(Productself).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.formcrispy }}
            <button type="submit">Search</button>
         </form>
    </div>

Shailesh Yadav

unread,
Jun 23, 2021, 9:29:10 AM6/23/21
to django...@googlegroups.com
Can you help with what ERROR you are getting? 

Thanks & Regards
Shailesh Yadav

   Linkedin 




--
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/CAFecadtwZG0%3DLwE989gsFkusgAnAGkvEspF3i5WC1-uWO6OOHw%40mail.gmail.com.

Aritra Ray

unread,
Jun 23, 2021, 9:29:44 AM6/23/21
to django...@googlegroups.com
This isn't working. When we are supplying a filter, the html is displaying all the products irrespective of the requested.

Shailesh Yadav

unread,
Jun 23, 2021, 9:32:46 AM6/23/21
to django...@googlegroups.com
Okay use below in html
{{yourmodelnameinsmall_filter.form}}

Thanks & Regards
Shailesh Yadav

   Linkedin 



Aritra Ray

unread,
Jun 23, 2021, 10:05:37 AM6/23/21
to django...@googlegroups.com
Okay, will try and let you know. 
Thank you for your response.

Aritra Ray

unread,
Jun 23, 2021, 3:44:14 PM6/23/21
to django...@googlegroups.com
Sorry, it's not working. I tried items_filter.form but it didn't work. Kindly check my views.py if that'll be of any help or require any changes.


Shailesh Yadav

unread,
Jun 23, 2021, 4:21:33 PM6/23/21
to django...@googlegroups.com
Okay.
If you using the Django filter then, please try like below in your views..

abc_list = ModelName.objects.all()
result_filter = ModelNameFilter(request.GET, queryset=abc_list)

Thanks & Regards
Shailesh Yadav

   Linkedin 



Shailesh Yadav

unread,
Jun 23, 2021, 4:24:44 PM6/23/21
to django...@googlegroups.com
in Html code 

<form method="get">
{{items_filter.form}}
<button type="submit" class="btn btn-warning btn-sm">
<span class="glyphicon glyphicon-search"></span> Search
</button>
</form>

Thanks & Regards
Shailesh Yadav

   Linkedin 



Aritra Ray

unread,
Jun 23, 2021, 4:31:12 PM6/23/21
to django...@googlegroups.com
Like this?

class Product(ListView):
    model = Items.objects.all()
    paginate_by = 6
    template_name = 'products.html'
    result_filter = ProductFilter(request.GET, queryset=model)

Aritra Ray

unread,
Jun 23, 2021, 4:34:27 PM6/23/21
to django...@googlegroups.com
Can you write the class based function of the above views.py. I'm unable to do so. Will be of great help.


Reply all
Reply to author
Forward
0 new messages