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 |
Another question - how can you filter by multiple column values without all of them being required?
Excelent !!!
Tried to follow but I stumbled in a 'Customer() got unexpected keyword arguments: 'data'' error.... any tip for me ?
Thanks !
Great! Thank you!
Thank you so much! This was super helpful
One small UI issue I have is the cripsy form re-renders every time
django-tables2triggers a GET request to sort by a particular column. Do you know of a way to cache the form so it doesn't appear glitchy to the user? My specific package versions are