Skip to content
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

Open
pep-un opened this issue Feb 26, 2023 · 1 comment
Open

Implement riche table #44

pep-un opened this issue Feb 26, 2023 · 1 comment
Labels
enhancement Impovement of existing feature
Milestone

Comments

@pep-un
Copy link
Owner

pep-un commented Feb 26, 2023

Implement sorted and filtered table.

https://django-tables2.readthedocs.io/en/latest/

@pep-un pep-un added the enhancement Impovement of existing feature label Feb 26, 2023
@pep-un pep-un added this to the Oxomium v3.0 milestone Aug 25, 2024
@pep-un
Copy link
Owner Author

pep-un commented Aug 25, 2024

Certainly! Here's how you can implement sortable, filterable tables in Django using ModelForm and ModelView, utilizing tools like Django Tables2 and Django Filter.

1. Django Tables2

Django Tables2 helps in creating HTML tables in Django with built-in support for sorting and pagination.

Installation

First, install Django Tables2:

pip install django-tables2

Configuration

Add 'django_tables2' to your INSTALLED_APPS in settings.py:

INSTALLED_APPS = [
    ...
    'django_tables2',
]

Model and Table

Create 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 Table class using django_tables2.Table:

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")

ModelView

You can then create a ModelView using Django's generic ListView combined with django_tables2.SingleTableMixin:

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"

Template

In your template, render the table using {% render_table %}:

{% load render_table from django_tables2 %}
{% render_table table %}

2. Django Filter

Django Filter adds filtering functionality to your Django views, which is particularly useful for list views.

Installation

Install Django Filter:

pip install django-filter

Configuration

Add 'django_filters' to your INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    'django_filters',
]

FilterSet

Create a FilterSet for your model:

import django_filters
from .models import ControlePoint

class ControlePointFilter(django_filters.FilterSet):
    class Meta:
        model = ControlePoint
        fields = ['name', 'description', 'created_at']

ModelView with Filtering

Combine django_filters.FilterView with Django's ListView:

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

Template

Render 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 Forms

To enhance the presentation of your filter forms (like those used with Django Filter), you can use Django Crispy Forms.

Installation

pip install django-crispy-forms

Configuration

Add 'crispy_forms' to INSTALLED_APPS and specify the style in settings.py:

INSTALLED_APPS = [
    ...
    'crispy_forms',
]

CRISPY_TEMPLATE_PACK = 'bootstrap4'

Putting it All Together

By 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 ModelForm and ModelView. Here's a summary of the setup:

  • Django Tables2: Used to create sortable and paginated tables.
  • Django Filter: Adds filtering functionality to your list views.
  • Django Crispy Forms: Enhances the appearance of filter forms.

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.

@pep-un pep-un modified the milestones: Oxomium v3.0, Oxomium v5.0 Aug 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Impovement of existing feature
Projects
None yet
Development

No branches or pull requests

1 participant