Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Customizing Django Templates Template Tags and Filters Handy Dandy Filters

Noah Fields
Noah Fields
13,985 Points

What if the word to pluralize has a non-standard plural (ie mouse and mice)?

Out of curiosity I briefly changed the word "step" to "cactus" to see whether the pluralize filter was smart enough to change based on the preceding word. I didn't expect it to work properly, and indeed, my result read, "There are 2 cactuss in this course: [etc.]" In such a scenario where I am using a word such as cactus(cacti), mouse(mice), or octopus(octopuses/octopi/octopodes, depending on how much I wanted to confuse the end user), what would the method to pluralize the word be?

1 Answer

David Evans
David Evans
10,490 Points

Hi Noah,

So I'm not familiar with django but I have worked with other templating engines.

I found this documentation that you might want to read: https://docs.djangoproject.com/en/2.0/ref/templates/builtins/

pluralizeยถ Returns a plural suffix if the value is not 1. By default, this suffix is 's'.

Example:

You have {{ num_messages }} message{{ num_messages|pluralize }}. If num_messages is 1, the output will be You have 1 message. If num_messages is 2 the output will be You have 2 >messages.

For words that require a suffix other than 's', you can provide an alternate suffix as a parameter to the filter.

Example:

You have {{ num_walruses }} walrus{{ num_walruses|pluralize:"es" }}. For words that donโ€™t pluralize by simple suffix, you can specify both a singular and plural suffix, separated by a comma.

Example:

You have {{ num_cherries }} cherr{{ num_cherries|pluralize:"y,ies" }}. Note

Use blocktrans to pluralize translated strings.

Specifically it seems you can do something like this:

You have {{ num_cherries }} cherr{{ num_cherries|pluralize:"y,ies" }}.

Hopefully this is what you're looking for and it helps.