Skip to main content

Create tables with sorting links on the headers in Django templates.

Project description

Django-table-sort

pre-commit.ci status Documentation Status codecov django-table-sort python-versions django-versions license Code style: black Contributor Covenant downloads

Create tables with sorting links on the headers in Django templates.

Documentation, including installation and configuration instructions, is available at https://django-table-sort.readthedocs.io/.

The Django Table Sort is released under the BSD license, like Django itself. If you like it, please consider contributing!

Installation

First, install with pip:

pip install django-table-sort

Second, add the app to your INSTALLED_APPS setting:

INSTALLED_APPS = [
    ...,
    "django_table_sort",
    ...,
]

Usage

First, add the static to your Template:

<link rel="stylesheet" href="{% static 'django_table_sort.css' %}"/>

django-sort-table uses by default Font Awesome 6 to display the icons, so you might need to add it too.

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.2/css/all.min.css" integrity="sha512-1sCRPdkRXhBV2PBLUdRb4tMg1w2YPf37qatUFeS7zlBy7jJI8Lf4VHwWfZZfpXtYSLy85pkm9GaYVYMfw5BC1A==" crossorigin="anonymous" referrerpolicy="no-referrer" />

Second, Use django-table-sort to display your tables.

In your view.py file:

class ListViewExample(ListView):
    model = Person
    template_name: str = "base.html"
    ordering_key = "o"

    def get_ordering(self) -> tuple:
        return self.request.GET.getlist(
            self.ordering_key, None
        )  # To make Django use the order

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["table"] = TableSort(
            self.request,
            self.object_list,
            sort_key_name=self.ordering_key,
            table_css_clases="table table-light table-striped table-sm",
        )
        return context

In your template.html file:

{{ table.render }}

Result:

The table is render with 2 link, one to Toggle the sort direction and another to remove the sort.

You can filter by each field you declare as a column.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_table_sort-0.5.0.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

django_table_sort-0.5.0-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file django_table_sort-0.5.0.tar.gz.

File metadata

  • Download URL: django_table_sort-0.5.0.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.10.9 Linux/5.15.0-1031-azure

File hashes

Hashes for django_table_sort-0.5.0.tar.gz
Algorithm Hash digest
SHA256 27436f8e1ca4e88b580bd75354e82b6690c499a6c9beeef3ed5653c8f1e8f144
MD5 e71ea40537f4b6606439b922508019de
BLAKE2b-256 f1a743c6e1535a083c148e8ea3b23b752940d2672623e60b92aada912874e2b8

See more details on using hashes here.

File details

Details for the file django_table_sort-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: django_table_sort-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.10.9 Linux/5.15.0-1031-azure

File hashes

Hashes for django_table_sort-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a5f6f3876b4683b18c0f4aeee6a1100ff96a1a020af89e493c9b497863d98550
MD5 e76360b195aa72ce83f74d4e1630d84d
BLAKE2b-256 216e4fd16de83c827389c505b04f90ee1c4075f4e34d7b0cf5557a09aec27256

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page