-
Notifications
You must be signed in to change notification settings - Fork 75
Example Usage
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.
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)
address = models.ForeignKey(Address)
tel = models.CharField('N Tel', max_length=30)
email = models.EmailField('Email', blank=True)
url = models.URLField('Web site', blank=True)
class Address(models.Model):
street = models.CharField('Street', max_length=30)
town = models.CharField('Ville', max_length=30)
postal_code = models.CharField('Postal code', max_length=30)
It’s pretty straightforward:
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),
)
{% extends "base.html" %}
{% load sorting_tags %}
{% block title %}{{ block.super }} • {{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 address__town Ville %}</th>
<th>{% anchor email E-mail %}</th>
<th>{% anchor url Website %}</th>
</tr>
{% autosort list %}
{% for item in list %}
<tr class="{% cycle 'even' 'odd' %}">
<td>{{item.denomination}}</td>
<td>{{item.name}}</td>
<td>{{item.last_name}}</td>
<td>{{item.tel}}</td>
<td>{{item.address.town}}</td>
<td>{{item.email}}</td>
<td>{{item.url}}</td>
</tr>
{% endfor %}
</table>
{% endblock %}