Skip to main content

Easy sorting of tables with Django

Project description

webstack-django-sorting allows for easy sorting, and sorting links generation without modifying your views. It is the perfect companion of linaro-django-pagination”,

There are really 5 steps to setting it up with your projects.

  1. List this application in the INSTALLED_APPS portion of your settings file. Your settings file might look something like:

    INSTALLED_APPS = (
        # ...
        'webstack_django_sorting',
    )
  2. Install the sorting middleware. Your settings file might look something like:

    MIDDLEWARE_CLASSES = (
        # ...
        'webstack_django_sorting.middleware.SortingMiddleware',
    )
  3. If it’s not already added in your setup, add the ‘request’ context processor:

    TEMPLATE_CONTEXT_PROCESSORS = (
        'django.contrib.auth.context_processors.auth',
        'django.core.context_processors.debug',
        'django.core.context_processors.i18n',
        'django.core.context_processors.media',
        'django.core.context_processors.static',
        'django.core.context_processors.tz',
        'django.core.context_processors.request',
        'django.contrib.messages.context_processors.messages',
    )

    This example comes from Django 1.6, take care to check against your Django version which context processors are supported (see global_settings.py file of your Django installation).

  4. Add this line at the top of your template to load the sorting tags:

    {% load sorting_tags %}
  5. 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 %}
  6. 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>

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

That’s it!

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

webstack-django-sorting-0.4.3.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

webstack_django_sorting-0.4.3-py2-none-any.whl (7.7 kB view details)

Uploaded Python 2

File details

Details for the file webstack-django-sorting-0.4.3.tar.gz.

File metadata

File hashes

Hashes for webstack-django-sorting-0.4.3.tar.gz
Algorithm Hash digest
SHA256 61aa2deb62453e9027e88a63c55508bc43db56263a202c8af4a7bda04e90a739
MD5 21d98fec83d8e887fb56aba4948047a9
BLAKE2b-256 97f672af0f27a30939aa04eb4d63793a6b22c1b33baf026511fde60e84763c5f

See more details on using hashes here.

File details

Details for the file webstack_django_sorting-0.4.3-py2-none-any.whl.

File metadata

File hashes

Hashes for webstack_django_sorting-0.4.3-py2-none-any.whl
Algorithm Hash digest
SHA256 72abd5ffb63cc8933e0f418d3cf4f8b23f9329eeb74bd925fa309f8193bbf95a
MD5 49533233a7058920629d3036ee020f6a
BLAKE2b-256 bbf6a8947fa12ccb0cc7b39591eb9baf5ff8160b95b44546df5fc46706ad933e

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