Integrates:
The code below displays a single paginated table with a simple filter.
Inspired by this article
Integrates:
The code below displays a single paginated table with a simple filter.
Inspired by this article
import django_filters | |
from myapp.models import ModelName | |
class ModelNameFilter(django_filters.FilterSet): | |
class Meta: | |
model = ModelName | |
fields = ['status'] |
from crispy_forms.helper import FormHelper | |
from crispy_forms.layout import Layout, Submit | |
class ModelNameFilterFormHelper(FormHelper): | |
form_method = 'GET' | |
layout = Layout( | |
'status', | |
Submit('submit', 'Apply Filter'), | |
) |
{% load render_table from django_tables2 %} | |
{% load crispy_forms_tags %} | |
<!-- display the filter --> | |
{% crispy filter.form filter.form.helper %} | |
<!-- display the table --> | |
{% render_table table %} |
from django_tables2 import Column, Table | |
from myapp.models import ModelName | |
class ModelNameTable(Table): | |
status = Column(accessor='status', verbose_name='Status') | |
class Meta: | |
model = ModelName |
from django_filters.views import FilterView | |
from django_tables2 import SingleTableMixin | |
class FilteredSingleTableView(SingleTableMixin, FilterView): | |
formhelper_class = None | |
def get_filterset(self, filterset_class): | |
kwargs = self.get_filterset_kwargs(filterset_class) | |
filterset = filterset_class(**kwargs) | |
filterset.form.helper = self.formhelper_class() | |
return filterset | |
class ModelNameView(FilteredSingleTableView): | |
template_name = 'modelname.html' | |
table_class = ModelNameTable | |
paginate_by = 25 | |
filterset_class = ModelNameFilter | |
formhelper_class = ModelNameFilterFormHelper |
Great! Thank you!