Skip to content
directeur edited this page Sep 13, 2010 · 4 revisions

django-sorting was very inspired by Eric Florenzano’s django-pagination , this latter consists of two tags and a middleware that, when used in a template, will automatically paginate your objects list. i.e. You don’t have to implement explicitely the “pagination” inside your view!

This app tries to complete django-pagination by offering the automatic sorting possibilities to your tabular data and this by giving you the ability to generate table headers <th> with links to sort your columns.

Just like django-pagination, this app needs you to edit a little bit your settings.py file, please reffer to the README file for explanations.

The example bellow will show show you how to, easily, integrate sorting to your template.

The Model

<pre> class Contact(models.Model): denomination = models.CharField('Denomination', max_length=200, blank=True) name = models.CharField('Nom', max_length=30) last_name = models.CharField('Prenom', max_length=30) town = models.CharField('Ville', max_length=30) tel = models.CharField('N Tel', max_length=30) email = models.EmailField('Email', blank=True) url = models.URLField('Web site', blank=True) </pre>

The View

It’s pretty straightforward:

<pre> def index(request): """ displays a list of contacts """ context = { 'title': 'Liste des Contacts' , 'list': Contact.objects.all(), }

return render_to_response( ‘contacts/list.html’, context, context_instance = RequestContext(request), )

The Template

<pre> {% extends "base.html" %} {% load sorting_tags %} {% block title %}{{ block.super }} &bull; {{title}} {% endblock %} {% block content %} <h2>{{title}}</h2> <table> <tr> <th>{% anchor denomination Dénomnination %}</th><!-- This is a TH tag with a link inside it --> <th>{% anchor name %}</th> <th>{% anchor last_name Prénom %}</th> <th>{% anchor tel Tel. %}</th> <th>{% anchor town Ville %}</th> <th>{% anchor email E-mail %}</th> <th>{% anchor url Website %}</th>

{% autosort list %} {% for item in list %} {{item.denomination}} {{item.name}} {{item.last_name}} {{item.tel}} {{item.town}} {{item.email}} {{item.url}} {% endfor %}

{% endblock %}

Clone this wiki locally