Usage

Sorting

The center piece of the app is the {% sort %} template tag which takes one required and one optional argument.

The bare bones example is:

{% load sorter_tags %}

{% sort object_qs as sorted_objects %}

{% for obj in sorted_objects %}
    {{ obj.title }}
{% endfor %}

If this template is rendered with a list of objects, say a Django QuerySet, the template tag will order it by calling the order_by(). The sorting criteria passed to this method is retrieved from the request’s GET paramaters (the “querystring”) by looking for a field-value pair with the name sort.

Simple

So imagine you have a list of blog posts that you want to sort by their creation dates, in an ascending order. With the example template above all you’d have to do is to call the view rendering those blog posts with the appropriate querystring:

http://example.com/blog/?sort=creation_date

Since name of the field is automatically picked up and passed to order_by() you can also sort in descending order by prepending the name of the field with a negative sign (-):

http://example.com/blog/?sort=-creation_date

You can also pass multiple fields to the querystring parameter to sort for multiple fields:

http://example.com/blog/?sort=-creation_date,title

Complex

In some cases you may want to sort multiple lists of objects in the same template or use advanced techniques like chained sorting workflows.

All you need to do is to pass a second parameter to the {% sort %} template tag which defines the name of the querystring parameter:

{% load sorter_tags %}

{% sort object_qs with "posts" as sorted_objects %}

{% for obj in sorted_objects %}
    {{ obj.title }}
{% endfor %}

Make sure to use unique names to prevent any clash with other sortings that may happen on the same page. As a matter of additional precaution the template tag will prepend it with 'sort_' when analyzing the request’s querystring.

So if you’d use the example template above, the template tag would look for a querystring parameter named 'sort_posts' because the second parameter to the template tag is "posts":

http://example.com/blog/?sort_posts=creation_date

Forms

Other than the sortlink template tag, django-sorter also ships with a second template tag to apply other sorting criterias – the sortform tag.

It works basically the same as sortlink and uses the same code behind the scenes, but looks for a different template: sorter/sortform.html. Just like the sortlink tag it’ll use the name of the querystring parameter if given to additionally look for a specific template, e.g. sorter/sortform_posts.html

An example:

{% sortform with "posts" by "creation_date" %}
    {% trans "Creation and title" %}
{% endsortform %}

rendered:

<form action="" method="get">
    <input type="hidden" name="sort_posts" value="creation_date" />
    <input type="submit" value="Creation date" title="Sort by: 'creation_date' (asc)" />
</form>

URLs

As a quick helper in case you don’t like django-sorter to generate the links or forms for your sorting efforts, you can also use the simple sorturl template tag:

{% sorturl with "posts" by "creation_date" %}

would only return the URL to the sorting:

/blog/?sort_posts=creation_date

Don’t forget that it also takes an optional as parameter (like the rest of the parameters described for the sort template tag). That’s great for storing the URL to further mangle it or use it for other template-y things, e.g.:

{% sorturl with "posts" by "creation_date" as sort_by_date_url %}

{% blocktrans with sort_by_date_url as url %}
Please visit the following URL to sort by date:

    http://example.com{{ sort_by_date_url }}

Thanks!
{% endblocktrans %}

Project Versions

Table Of Contents

Previous topic

django-sorter

Next topic

Settings

This Page