I just learned about django.contrib.humanize:
Seems useful - however, what if I want to access that functionality outside the context of a template? Is there a library supporting that one which is just plain functions and classes?
I just learned about django.contrib.humanize:
Seems useful - however, what if I want to access that functionality outside the context of a template? Is there a library supporting that one which is just plain functions and classes?
A filter is a Python function that accepts a value and (optionally) a parameter. (See How to create custom template tags and filters | Django documentation | Django)
You can import and use those functions directly.
For example, this works:
>>> from django.contrib.humanize.templatetags import humanize
>>> humanize.ordinal(6)
'6th'
>>> humanize.intword(123456789)
'123.5 million'
You’re not limited to the humanize library either. This also works:
>>> from django.template import defaultfilters
>>> print(defaultfilters.filesizeformat(6543210))
6.2 MB