This site runs best with JavaScript enabled.

Django Pluralize Outside of Templates


How to use the pluralize filter directly in python

The pluralize filter is great for humanizing text in templates. But what if you want to use the same function inside of your python code. For example, let's say you are sending back a message via ajax.

It is simple enough to make use of the same pluralize filter function in your python code:

1from django.template.defaultfilters import pluralize
2
3def message(count):
4 return '{} item{} {} updated.'.format( count, pluralize(count), pluralize(count, 'was,were'))

Testing this would produce the following results:

1In [3]: message(1) Out[3]: '1 item was updated.'
2
3In [4]: message(2) Out[4]: '2 items were updated.'
4
5In [5]: message(0) Out[5]: '0 items were updated.'

Discuss on TwitterEdit post on GitHub

Share article
Dustin Davis

Dustin Davis is a software engineer, people manager, hacker, and entreprenuer. He loves to develop systems and automation. He lives with his wife and five kids in Utah.

Join the Newsletter



Dustin Davis