Usage¶
Configuration¶
Include django_sorting_bootstrap in your INSTALLED_APPS
Put {% load sorting_tags %} at top of your templates.
Your templates have four tags available:
auto_sort
sort_link
sort_th
sort_headers
Basic usage¶
{% auto_sort queryset %}
{% sort_link "link text" "field_name" %}
{% sort_th "link text" "field_name" %}
{% sort_headers simpleschangelist %}
Django views¶
For sorting to work, your views have to
add the current sort field to the context
apply the sorting to the queryset
For a generic ListView, this could be done as follows:
from django.views.generic import ListView
class ExampleListView(ListView):
model = MyModel
def get_context_data(self, **kwargs):
# add current sort field to context
c = super(ExampleListView, self).get_context_data(**kwargs)
if "sort_by" in self.request.GET:
c["current_sort_field"] = self.request.GET.get("sort_by")
return c
def get_queryset(self):
# apply sorting
qs = super(ExampleListView, self).get_queryset()
if "sort_by" in self.request.GET:
qs = qs.order_by(self.request.GET.get("sort_by"))
return qs