Package: webstack-django-sorting
webstack-django-sortingWhat?webstack-django-sortingis 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 ofdjango-pagination.There are other powerful projects to sort tables such asdjango-tables2but I don't like the
high levelrender_tabletag because it requires to define the CSS inTableclasses or to write custom templates.A demonstration of the features is provided intestprojdirectory. The filetestproj/README.mdprovides information on how to use it.FeaturesDjango or Jinja2 templatesDjango ORM or Python sortingSwitches between ascending, descending, and no sortingProvides links to sort on different criterionsVisual feedback on applied orderingSupports 3.6+Supports translation of link titlesTo upgrade towebstack-django-sortingv1.0.0+, you must remove the old middlewarewebstack_django_sorting.middleware.SortingMiddlewarefromMIDDLEWARE_CLASSESlist.How to use it in your projectThe provide is available on PyPI:pipinstallwebstack_django_sortingThe project provides examples of integration with Django and Jinja2 templates.For Django templatesAdd the application to theINSTALLED_APPSlist:INSTALLED_APPS=[# ...'webstack_django_sorting',]Check the request context processor is loaded inTEMPLATESoptions: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 %}You can pass the optionnulls=first(ornulls=last) to explicitly define
the ordering of NULL (not supported by all databases,Indexing ASC, DESC and NULLS FIRST/LAST)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><ahref="/path/to/your/view/?sort=first_name"title="Nom">Nom</a></th><th><ahref="/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 %}.For Jinja2 templatesDefine the environment in theTEMPLATESoptions:TEMPLATES={{"BACKEND":"django.template.backends.jinja2.Jinja2","DIRS":[],"APP_DIRS":True,"OPTIONS":{"environment":"testproj.testapp.jinja2.env.JinjaEnvironment",},},]Your environment file should addsorting_anchorandsort_querysetto globals:fromjinja2.environmentimportEnvironmentfromwebstack_django_sorting.jinja2_globalsimportsorting_anchor,sort_querysetclassJinjaEnvironment(Environment):def__init__(self,**kwargs):super().__init__(**kwargs)self.globals["sorting_anchor"]=sorting_anchorself.globals["sort_queryset"]=sort_querysetNow, you can generate header links to sort your queryset.<tr><th>{{ sorting_anchor(request, "created_on", "Date") }}</th><!--...--><tr>The queryset should be wrapped withsort_querysetto use the GET request arguments for sorting:{% for secret_file in sort_queryset(request, secret_files) %}<!--...-->{% endfor %}That's it!SettingsThe library provides a few settings that you can define in the Django settings of your project:DEFAULT_SORT_UP, the HTML character to display the up symbol in the column headers (' ↑' by default).DEFAULT_SORT_DOWN, the HTML character to display the down symbol in the column headers (' ↓' by default).SORTING_INVALID_FIELD_RAISES_404, if true, a 404 response will be returned on invalid use of query parameters (false by default).