Wednesday, June 23, 2021

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

Like this?

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

On Wed, 23 Jun 2021 at 21:51, Shailesh Yadav <shaileshyadav7958@gmail.com> wrote:
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
+91-9920886044

  Linkedin 




On Wed, Jun 23, 2021 at 9:13 PM Aritra Ray <aritra805@gmail.com> wrote:
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.


On Wed, 23 Jun 2021 at 15:02, Shailesh Yadav <shaileshyadav7958@gmail.com> wrote:
Okay use below in html
{{yourmodelnameinsmall_filter.form}}

Thanks & Regards
Shailesh Yadav
+91-9920886044

  Linkedin 




On Wed, Jun 23, 2021 at 2:58 PM Aritra Ray <aritra805@gmail.com> wrote:
This isn't working. When we are supplying a filter, the html is displaying all the products irrespective of the requested.

On Wed, 23 Jun, 2021, 2:57 pm Shailesh Yadav, <shaileshyadav7958@gmail.com> wrote:
Can you help with what ERROR you are getting? 

Thanks & Regards
Shailesh Yadav
+91-9920886044

  Linkedin 




On Wed, Jun 23, 2021 at 2:25 PM Aritra Ray <aritra805@gmail.com> wrote:
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>

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

--
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/CAMQ-AEXsJ%2B19X3aO740%3DD%2BZUhcEYujh2FWScZDXCqM4cB0Bikg%40mail.gmail.com.

--
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/CAFecaduw28greFLXGqsTuit5JmCrzfHqawOU9HKvoojfXQLYtg%40mail.gmail.com.

--
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/CAMQ-AEXrqujnhn3q_629ZyjCbrS31j7mtqAt9br0psKPSB53dQ%40mail.gmail.com.

--
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/CAFecadtSYhFwnu_sA1dtz6v7ym80G%3Db4Aj2fkkgrYysJk9sLEA%40mail.gmail.com.

--
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/CAMQ-AEWAhXmpLBnm0oxSwKY%3DSH2WuR%2BQMrbcLSOcY4OgE%2B-e%2Bg%40mail.gmail.com.

--
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/CAFecadvuuDocq5aSjucx2yA9V0jyg6jWOzQ5MZywpV2ZChXzVA%40mail.gmail.com.

No comments:

Post a Comment