pluralize Filter

String Most Useful

Argument: suffix (Optional) ā€“ the suffix to use for pluralization. Default is s.

Documentation

If the value is not 1, '1', or an object of length 1, the pluralize filter outputs an ā€œsā€ or the value of the suffix argument if one is used.

Variable

classes = {
    'Python': [
        'Intro Python', 'Advanced Python', 'Data Science', 'Django'
    ],
    'Databases': [
        'Intro PostgreSQL', 'Intro MySQL', 'Intro SQL Server', 'Intro Oracle'
    ],
    'Web': [
        'HTML', 'CSS', 'JavaScript'
    ],
    'XML': [
        'Intro XML'
    ]
}

Template

<ol>
  {% for category, titles in classes.items %}
    <li>
      {{ category }}: {{ titles|length }}
      class{{ titles|pluralize:"es" }}
    </li>
  {% endfor %}
</ol>

Result

<ol>
  <li>Python: 4 classes</li>
  <li>Databases: 4 classes</li>
  <li>Web: 3 classes</li>
  <li>XML: 1 class</li>
</ol>

Commentary

Super useful. Much easier and cleaner than using conditional expressions to decide when to pluralize a word.


Did we get something wrong? Is there a use case for the pluralize filter that we should add? Please let us know.

Send Feedback

Official Documentation
This page last updated on Oct. 30, 2022, 1:21 p.m. EST