it works like an original "pluralize" filter, but it need argument with 3 parts, splitted by comma.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | # -*- coding: utf-8 -*-
from django.template import Library, TemplateSyntaxError
from django.template.defaultfilters import stringfilter
register = Library()
@register.filter
@stringfilter
def rupluralize(value, arg):
bits = arg.split(u',')
try:
if str(value).endswith('1'):
return bits[0]
elif str(value)[-1:] in '234':
return bits[1]
else:
return bits[2]
except:
raise TemplateSyntaxError
return ''
rupluralize.is_safe = False
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
This filter will not work for numbers that end in 11, 12, 13, and 14. For example:
Something like this would work better:
#
Please login first before commenting.