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.
List this application in the INSTALLED_APPS portion of your settings file. Your settings file might look something like:
INSTALLED_APPS = ( # ... 'webstack_django_sorting', )
Install the sorting middleware. Your settings file might look something like:
MIDDLEWARE_CLASSES = ( # ... 'webstack_django_sorting.middleware.SortingMiddleware', )
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).
Add this line at the top of your template to load the sorting tags:
{% load sorting_tags %}
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 %}
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 | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for webstack-django-sorting-0.4.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | a11434936059e26cc433dcde9a18887a8e32623645498c44b71d96397c4642dc |
|
MD5 | c5e8422515b04169d504607a4cdba829 |
|
BLAKE2b-256 | 6fc65801d29ce382826c5c243362507d65449a0e78f06d28a995c3ef0177cf70 |
Hashes for webstack_django_sorting-0.4.2-py2-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5cb961629e682b03e2e661a3a24d555c0465f3970490534bd2efb3befc6b9cc6 |
|
MD5 | daa2686512eae348f2dc3b22c5d65f40 |
|
BLAKE2b-256 | d351ea8a92822134ad6eaeffb767993f1bef3eef0e454a21ddc67fa2feb5b897 |