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
pip install webstack_django_sorting
Add the application to the INSTALLED_APPS list:
INSTALLED_APPS = [ # ... 'webstack_django_sorting', ]
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', # ... ], }, }, ]
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
File details
Details for the file webstack-django-sorting-1.0.1.tar.gz
.
File metadata
- Download URL: webstack-django-sorting-1.0.1.tar.gz
- Upload date:
- Size: 6.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b5df9f5f9d689daa255e3272cc4f55791d10f13d19c6e3c90f56bbf5f5a9f152 |
|
MD5 | f5407f58294cec624e51e9a6131535cb |
|
BLAKE2b-256 | 609ccd5ff9e8c436c4781ead9bfb3555e7b768eb144f7db849435b9736321550 |
File details
Details for the file webstack_django_sorting-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: webstack_django_sorting-1.0.1-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a74e1a8b3e85b9e85f8c9d51f0beda90cb42f19bc1c7b01f78b1b6ebd77864cc |
|
MD5 | 9120b39f5e0fe9c47facf4e99009f70b |
|
BLAKE2b-256 | e47abe09abcf8526ecab9241b15f48aa2dfd0abfddcb444dde1342a9b3db1224 |