Django Numerical Positions

November 12, 2010

I wanted to convert the position at a competition into 1st, 2nd, 3rd, 4th etc. Django makes this easy with the humanize template filters.

First of all, you need to add the humanize application to your INSTALLED_APPS:

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.humanize',
)

Next, add:

{% load humanize %}

to the top of your template, then use the ordinal filter:

<h3>{{result.results_position|ordinal}} {{result.contest_event.event_date}} {{result.contest_event.contest.name}}</h3>

On the resulting HTML, this will show as