Django: Django_filters in a class-based views

I ask you if you know how put this filters :

class CoursesFilters(django_filters.FilterSet):
    class Meta:
        model   =  Courses
        exclude = ('description')  

in this class view :

class CoursesList(ListView):
    model         = Courses    
    template_name = 'courses_list.html'  

I used to build my applications using function-based views, and this is my first time use class-based views.
Any idea?

>Solution :

django-filters has a FilterView [readthedocs.io] that can be used:

from django_filters.views import FilterView

class CoursesList(FilterView):
    model = Courses    
    template_name = 'courses_list.html' 
    filterset_class = CoursesFilters

The filterset_class specifies the FilterSet that. The filter is passed to the template as filter. You thus can render a {{ filter.form }} in the template.

Leave a Reply