-
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.
<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>
It’s pretty straightforward:
<pre>
def index(request):
""" displays a list of contacts """
context = {
'title': 'Liste des Contacts' ,
'list': Contact.objects.all(),
}
<pre>
{% 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 town Ville %}</th>
<th>{% anchor email E-mail %}</th>
<th>{% anchor url Website %}</th>
{% endblock %}