-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement riche table #44
Comments
Certainly! Here's how you can implement sortable, filterable tables in Django using 1. Django Tables2Django Tables2 helps in creating HTML tables in Django with built-in support for sorting and pagination. InstallationFirst, install Django Tables2: pip install django-tables2 ConfigurationAdd INSTALLED_APPS = [
...
'django_tables2',
] Model and TableCreate your model, if you haven't already: from django.db import models
class ControlePoint(models.Model):
name = models.CharField(max_length=255)
description = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.name Next, define a import django_tables2 as tables
from .models import ControlePoint
class ControlePointTable(tables.Table):
class Meta:
model = ControlePoint
template_name = "django_tables2/bootstrap.html"
fields = ("name", "description", "created_at") ModelViewYou can then create a from django_tables2.views import SingleTableView
from .models import ControlePoint
from .tables import ControlePointTable
class ControlePointListView(SingleTableView):
model = ControlePoint
table_class = ControlePointTable
template_name = "controle_point_list.html" TemplateIn your template, render the table using {% load render_table from django_tables2 %}
{% render_table table %} 2. Django FilterDjango Filter adds filtering functionality to your Django views, which is particularly useful for list views. InstallationInstall Django Filter: pip install django-filter ConfigurationAdd INSTALLED_APPS = [
...
'django_filters',
] FilterSetCreate a import django_filters
from .models import ControlePoint
class ControlePointFilter(django_filters.FilterSet):
class Meta:
model = ControlePoint
fields = ['name', 'description', 'created_at'] ModelView with FilteringCombine from django_filters.views import FilterView
from django_tables2.views import SingleTableMixin
from .models import ControlePoint
from .tables import ControlePointTable
from .filters import ControlePointFilter
class ControlePointListView(SingleTableMixin, FilterView):
model = ControlePoint
table_class = ControlePointTable
template_name = "controle_point_list.html"
filterset_class = ControlePointFilter TemplateRender the filter form and the table in your template: <form method="get">
{{ filter.form.as_p }}
<button type="submit">Filter</button>
</form>
{% render_table table %} 3. Django Crispy FormsTo enhance the presentation of your filter forms (like those used with Django Filter), you can use Django Crispy Forms. Installationpip install django-crispy-forms ConfigurationAdd INSTALLED_APPS = [
...
'crispy_forms',
]
CRISPY_TEMPLATE_PACK = 'bootstrap4' Putting it All TogetherBy combining Django Tables2, Django Filter, and optionally Django Crispy Forms, you can create powerful, user-friendly, sortable, and filterable tables within your Django project using
These tools seamlessly integrate with Django's class-based views and provide a comprehensive solution for displaying and interacting with data in a Django application. |
Implement sorted and filtered table.
https://django-tables2.readthedocs.io/en/latest/
The text was updated successfully, but these errors were encountered: