Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading