How to add django-filter Rangefilter class?

43 Views Asked by At

I am try to add bootstrap class in to price range filter but i can't,

import django_filters
from panel.models import Product, Category,Size,TopNote, BaseNote, MidNote
from django import forms

class ProductFilter(django_filters.FilterSet):
 price = django_filters.RangeFilter(
            # widget = forms.TextInput(attrs={'class':'form-control'})
    )
 class Meta:
        model = Product
        fields =['name',
                 'category_id',
                 'price',
                 'top_notes', 'base_notes', 'category_id', 'size']

when I am add class using widget = forms.TextInput(attrs={'class':'form-control'}) range filter convert into 1 textbox i dont't know why ???/

1

There are 1 best solutions below

2
Marco On

The RangeFilter filter does not use the TextInput widget. Instead, it uses the RangeWidget widget to produce more than one input field.

from django_filters.widgets import RangeWidget

class ProductFilter(django_filters.FilterSet):
    price = django_filters.RangeFilter(
        widget = RangeWidget(attrs={'class':'form-control'})
    )
    ...

The documentation states::

RangeWidget – this widget is used with RangeFilter to generate two form input elements using a single field.

Read also the documentation about RangeWidget.