Skip to main content

webstack-django-sorting

What?

webstack-django-sorting is a Django app which allows for easy sorting of data tables. You don't need to change anything to your views to use it. It provides sorting links for table headers. It is the perfect companion of django-pagination.

There are other powerful projects to sort tables such as django-tables2 but I don't like the high level render_table tag because it requires to define the CSS in Table classes or to write custom templates.

A demonstration of the features is provided in testproj directory. The file testproj/README.md provides information on how to use it.

Features

  • Django or Jinja2 templates
  • Django ORM or Python sorting
  • Switches between ascending, descending, and no sorting
  • Provides links to sort on different criterions
  • Visual feedback on applied ordering
  • Supports 3.6+
  • Supports translation of link titles

To upgrade to webstack-django-sorting v1.0.0+, you must remove the old middleware webstack_django_sorting.middleware.SortingMiddleware from MIDDLEWARE_CLASSES list.

How to use it in your project

The package is available on PyPI:

uv add webstack-django-sorting

For Jinja2 template support, install with the optional dependency:

uv add webstack-django-sorting[jinja2]

The project provides examples of integration with Django and Jinja2 templates.

For Django templates

  1. Add the application to the INSTALLED_APPS list:

    INSTALLED_APPS = [
        # ...
        'webstack_django_sorting',
    ]
    
  2. Check the request context processor is loaded in TEMPLATES options:

    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [],
            'APP_DIRS': True,
            'OPTIONS': {
                'context_processors': [
                    # ...
                    'django.template.context_processors.request',
                    # ...
                ],
            },
        },
    ]
    
  3. Add this line at the top of your template to load the sorting tags:

    {% load sorting_tags %}
    
  4. Decide on a variable that you would like to sort, and use the autosort tag on that variable before iterating over it:

    {% autosort object_list %}
    

    You can pass the option nulls=first (or nulls=last) to explicitly define the ordering of NULL (not supported by all databases, Indexing ASC, DESC and NULLS FIRST/LAST)

  5. Now, you want to display different headers with links to sort your objects_list:

    <tr>
      <th>{% anchor first_name _("Name") %}</th>
      <th>{% anchor creation_date _("Creation") %}</th>
    </tr>
    

    The first argument is a field or an attribute of the objects list, and the second one (optional) is a title that would be displayed. The previous snippet will be rendered like this in French:

    <tr>
      <th><a href="/path/to/your/view/?sort=first_name" title="Nom">Nom</a></th>
      <th>
        <a href="/path/to/your/view/?sort=creation_date" title="Création"
          >Création</a
        >
      </th>
    </tr>
    

    An optional 3rd argument allows you to sort first by descending (e.g. show most recent dates first) {% anchor some_date _("Date") desc %}

    If your application doesn't support internationalization, you can use a simple {% anchor first_name Name %}.

For Jinja2 templates

  1. Define the environment in the TEMPLATES options:

    TEMPLATES = {
        {
            "BACKEND": "django.template.backends.jinja2.Jinja2",
            "DIRS": [],
            "APP_DIRS": True,
            "OPTIONS": {
                "environment": "testproj.testapp.jinja2.env.JinjaEnvironment",
            },
        },
    ]
    
  2. Your environment file should add sorting_anchor and sort_queryset to globals:

    from jinja2.environment import Environment
    from webstack_django_sorting.jinja2_globals import sorting_anchor, sort_queryset
    
    class JinjaEnvironment(Environment):
        def __init__(self, **kwargs):
            super().__init__(**kwargs)
            self.globals["sorting_anchor"] = sorting_anchor
            self.globals["sort_queryset"] = sort_queryset
    
  3. Now, you can generate header links to sort your queryset.

    <tr>
      <th>{{ sorting_anchor(request, "created_on", "Date") }}</th>
      <!--...-->
    </tr>
    
    <tr></tr>
    
  4. The queryset should be wrapped with sort_queryset to use the GET request arguments for sorting:

    {% for secret_file in sort_queryset(request, secret_files) %}
    <!--...-->
    {% endfor %}
    

That's it!

Settings

The library provides a few settings that you can define in the Django settings of your project:

Sort indicators

By default, sort direction is shown using HTML entities (arrows):

  • DEFAULT_SORT_UP, the HTML character to display the up symbol (' ↑' by default).
  • DEFAULT_SORT_DOWN, the HTML character to display the down symbol (' ↓' by default).

Alternatively, you can use CSS classes for more flexible styling:

  • SORTING_CSS_CLASS_ASC, CSS class added to the anchor when sorted ascending (empty by default).
  • SORTING_CSS_CLASS_DESC, CSS class added to the anchor when sorted descending (empty by default).

Example with CSS classes:

# settings.py
SORTING_CSS_CLASS_ASC = "sorted-asc"
SORTING_CSS_CLASS_DESC = "sorted-desc"

This will produce <a class="sorted-asc" ...> when sorted ascending, allowing you to style the indicator with CSS:

.sorted-asc::after { content: " \2191"; }  /* Up arrow */
.sorted-desc::after { content: " \2193"; }  /* Down arrow */

Error handling

  • SORTING_INVALID_FIELD_RAISES_404, if true, a 404 response will be returned on invalid use of query parameters (false by default).

Default Sort Direction

By default, clicking a column header sorts ascending first. You can change this per-column to sort descending on first click (useful for date columns where you typically want most recent first):

Django template:

{% anchor created_date _("Created") "desc" %}

Jinja2 template:

{{ sorting_anchor(request, "created_date", "Created", "desc") }}

Performance Considerations

The library uses Django ORM's order_by() for database fields, which is efficient. However, when sorting by model properties or computed attributes (not database fields), it falls back to Python sorting which loads all objects into memory.

For large querysets, ensure you're sorting by database fields only. You can check if a field will use Python sorting:

from webstack_django_sorting.common import need_python_sorting

# Returns True if Python sorting will be used (slower)
need_python_sorting(queryset, "my_property")

If you must sort by a computed value on large datasets, consider:

  • Adding a database field to store the computed value
  • Using database-level annotations
  • Limiting the queryset size before sorting

Release files for webstack-django-sorting 3.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for webstack-django-sorting 3.1.1
File Size Uploaded
webstack_django_sorting-3.1.1.tar.gz 50.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for webstack-django-sorting 3.1.1
File Interpreter ABI Platform
webstack_django_sorting-3.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 61.8 kB

Release files / webstack_django_sorting-3.1.1.tar.gz

Download URL webstack_django_sorting-3.1.1.tar.gz
Size 50.9 kB
Tags Source
SHA-256 checksum
How to use checksums
9cbf500b8b45b5989249d9a1f5df63c1c4a8c395c5ee552d10ed65c0c2b8c725
BLAKE2b-256 checksum
How to use checksums
a5b10ecb6fbad542d6604fe0bfd53a01ff9e3846dcbbdf62b8c93316bd7f26b7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / webstack_django_sorting-3.1.1-py3-none-any.whl

Download URL webstack_django_sorting-3.1.1-py3-none-any.whl
Size 10.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
93d91f7b9fab4d372679fd128ca169b45b6176e06009be045081e2250a79214a
BLAKE2b-256 checksum
How to use checksums
8971097c26b8649c47940cf1802b12ff08614479edb53ea92beb16c677f5a1de
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

3.1.1 This release

2 release files

3.1.0

2 release files

3.0.4

2 release files

3.0.3

2 release files

3.0.2

2 release files

3.0.1

2 release files

3.0.0

2 release files

2.4.0

2 release files

2.3.1

2 release files

2.3.0

2 release files

2.2.1

2 release files

2.1.1

2 release files

2.1.0

2 release files

2.0.3

2 release files

2.0.2

2 release files

2.0.1

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.4.4

2 release files

0.4.3

2 release files

0.4.2

2 release files

0.4.1

1 release file

0.4

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page