From f31f419c9d00fb5f3ea31f1c24e8944598b641f7 Mon Sep 17 00:00:00 2001 From: "thi.guten" Date: Mon, 21 Aug 2017 13:01:18 +0200 Subject: [PATCH 1/4] Update README.rst --- README.rst | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/README.rst b/README.rst index 7a2d1d3..6c90b9c 100644 --- a/README.rst +++ b/README.rst @@ -92,15 +92,19 @@ sort_headers This function is somewhat more complicated to use, but it builds the whole table headers for sorting. In order to use it you have to pass in your view a SimplesChangeList (from sorting_bootstrap.views). Let's have an exemple using a view extending Generic ListView. -Basic usage:: +Basic usage +----------- + +:: from django.views.generic import ListView + from sorting_bootstrap.views import SimpleChangeList + + class MyView(ListView) - def get_context_data(self, **kwargs): - # Calls the base implementation first to get a context - context = super(self.__class__, self).get_context_data(**kwargs) - - from sorting_bootstrap.views import SimpleChangeList + def get_context_data(self, **kwargs): + # Calls the base implementation first to get a context + context = super(self.__class__, self).get_context_data(**kwargs) # Gets the fields that are going to be in the headers list_display = [i.name for i in self.model._meta.fields] # Doesnt show ID field @@ -108,6 +112,7 @@ Basic usage:: cl = SimpleChangeList(self.request, self.model, list_display) # Pass a change list to the views context['cl'] = cl + return context You also need to call the function in your template:: From b1bf82c024f09eb587b435ad18c22293f5afafbe Mon Sep 17 00:00:00 2001 From: "thi.guten" Date: Mon, 21 Aug 2017 13:03:43 +0200 Subject: [PATCH 2/4] Update README.rst --- README.rst | 4 ---- 1 file changed, 4 deletions(-) diff --git a/README.rst b/README.rst index 6c90b9c..0f65dd2 100644 --- a/README.rst +++ b/README.rst @@ -92,15 +92,11 @@ sort_headers This function is somewhat more complicated to use, but it builds the whole table headers for sorting. In order to use it you have to pass in your view a SimplesChangeList (from sorting_bootstrap.views). Let's have an exemple using a view extending Generic ListView. -Basic usage ------------ - :: from django.views.generic import ListView from sorting_bootstrap.views import SimpleChangeList - class MyView(ListView) def get_context_data(self, **kwargs): # Calls the base implementation first to get a context From e7b167b3a7e062fb5e3cb1d6b59f6f2d1165c5e6 Mon Sep 17 00:00:00 2001 From: Markus Thielen Date: Tue, 22 Aug 2017 10:30:13 +0200 Subject: [PATCH 3/4] added German translation --- .../locale/de/LC_MESSAGES/django.mo | Bin 0 -> 439 bytes .../locale/de/LC_MESSAGES/django.po | 26 ++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 sorting_bootstrap/locale/de/LC_MESSAGES/django.mo create mode 100644 sorting_bootstrap/locale/de/LC_MESSAGES/django.po diff --git a/sorting_bootstrap/locale/de/LC_MESSAGES/django.mo b/sorting_bootstrap/locale/de/LC_MESSAGES/django.mo new file mode 100644 index 0000000000000000000000000000000000000000..5dcdfc5be605878578785ab1e0134dd333b6e75d GIT binary patch literal 439 zcmYL^Pfx-y7>6}_+R?Lz*TjPcUmUwQ3o}esaB*W~45D64g*sc6cW6NaQux~gt>=;fAO, YEAR. +# +#, fuzzy +msgid "" +msgstr "" +"Project-Id-Version: PACKAGE VERSION\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2017-08-22 10:20+0200\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" +"Language: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" +"X-Language: de_DE\n" + +#: templates/sorting_bootstrap/sort_headers_frag.html:7 +#: templates/sorting_bootstrap/sort_th_frag.html:5 +#, fuzzy +msgid "Toggle sorting" +msgstr "Sortierung umkehren" From 43edabd0a812cf59f9fef49b6941cf70508ab6b1 Mon Sep 17 00:00:00 2001 From: Markus Thielen Date: Tue, 22 Aug 2017 11:03:37 +0200 Subject: [PATCH 4/4] updated README with examples --- README.rst | 73 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 55 insertions(+), 18 deletions(-) diff --git a/README.rst b/README.rst index 0f65dd2..d17ba3f 100644 --- a/README.rst +++ b/README.rst @@ -25,27 +25,63 @@ To install ``django-sorting-bootstrap`` simply run:: Configuration ------------- -Include `sorting_bootstrap` in your `INSTALLED_APPS` +Include ``sorting_bootstrap`` in your ``INSTALLED_APPS`` -Put `{% load sorting_tags %}` at top of your templates. +Put ``{% load sorting_tags %}`` at top of your templates. Your templates have four tags available: -`auto_sort` -`sort_link` -`sort_th` -`sort_headers` +``auto_sort`` +``sort_link`` +``sort_th`` +``sort_headers`` -Basic usage:: +Basic usage +------------------- + +:: {% auto_sort queryset %} {% sort_link "link text" "field_name" %} {% sort_th "link text" "field_name" %} {% sort_headers simpleschangelist %} - -auto_sort + +Django views +------------------- + +For sorting to work, your views have to + +1. add the current sort field to the context +2. 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 + +Template Tags ------------------- + + +auto_sort +^^^^^^^^^^^^^^^^^^ It sorts the queryset in place and replaces the queryset by the sorted queryset. This needs to be called prior to a slice has been taken from a queryset. @@ -58,7 +94,7 @@ Basic usage:: sort_link ------------------ +^^^^^^^^^^^^^^^^^^ Sort link outputs a link which will sort on the given field. The field to sort on should be a database field, or something which `.order_by` of queryset would work. @@ -67,19 +103,19 @@ Basic usage:: {% sort_link "link text" "field_name" %} Example usage:: - + {% sort_link "Name" "name" %} - + It may also be used as:: - + {% sort_link "link text" "field_name" "vis_name" %} {% sort_link "Name" "name" "what" %} - + This is useful if you do not wnat to expose your database fields in urls. sort_th -------------------- +^^^^^^^^^^^^^^^^^^ It works the same way as sort_link, but the difference is the output template that renders a table header tag using `Bootstrap`_ classes and Glyphicons. Basic usage:: @@ -88,7 +124,7 @@ Basic usage:: sort_headers -------------------- +^^^^^^^^^^^^^^^^^^ This function is somewhat more complicated to use, but it builds the whole table headers for sorting. In order to use it you have to pass in your view a SimplesChangeList (from sorting_bootstrap.views). Let's have an exemple using a view extending Generic ListView. @@ -96,11 +132,12 @@ Let's have an exemple using a view extending Generic ListView. from django.views.generic import ListView from sorting_bootstrap.views import SimpleChangeList - + class MyView(ListView) + def get_context_data(self, **kwargs): # Calls the base implementation first to get a context - context = super(self.__class__, self).get_context_data(**kwargs) + context = super(self.__class__, self).get_context_data(**kwargs) # Gets the fields that are going to be in the headers list_display = [i.name for i in self.model._meta.fields] # Doesnt show ID field