Skip to main content
Help us improve PyPI by participating in user testing. All experience levels needed!

Easy sorting of tables with Django

Project description

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.

A demonstration of the features is provided in testproj directory.

Features

  • 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 Python 2.7, 3.5 and 3.6
  • Supports translation of link titles

Upgrade from v0.5.0 and below

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

  1. pip install webstack_django_sorting

  2. Add the application to the INSTALLED_APPS list:

    INSTALLED_APPS = [
        # ...
        'webstack_django_sorting',
    ]
    
  3. 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',
                    # ...
                ],
            },
        },
    ]
    
  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


Release history Release notifications

This version
History Node

1.0.1

History Node

1.0.0

History Node

0.4.4

History Node

0.4.3

History Node

0.4.2

History Node

0.4.1

History Node

0.4

Download files

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

Filename, size & hash SHA256 hash help File type Python version Upload date
webstack_django_sorting-1.0.1-py3-none-any.whl (8.1 kB) Copy SHA256 hash SHA256 Wheel 3.6 Mar 1, 2017
webstack-django-sorting-1.0.1.tar.gz (6.4 kB) Copy SHA256 hash SHA256 Source None Mar 1, 2017

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page