2016-01-25 05:26:11 +08:00
|
|
|
===========================
|
|
|
|
``django.contrib.humanize``
|
|
|
|
===========================
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
.. module:: django.contrib.humanize
|
|
|
|
:synopsis: A set of Django template filters useful for adding a "human
|
|
|
|
touch" to data.
|
|
|
|
|
|
|
|
A set of Django template filters useful for adding a "human touch" to data.
|
|
|
|
|
|
|
|
To activate these filters, add ``'django.contrib.humanize'`` to your
|
|
|
|
:setting:`INSTALLED_APPS` setting. Once you've done that, use
|
2010-11-29 13:30:11 +08:00
|
|
|
``{% load humanize %}`` in a template, and you'll have access to the following
|
|
|
|
filters.
|
|
|
|
|
|
|
|
.. templatefilter:: apnumber
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``apnumber``
|
|
|
|
============
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
For numbers 1-9, returns the number spelled out. Otherwise, returns the
|
|
|
|
number. This follows Associated Press style.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* ``1`` becomes ``one``.
|
|
|
|
* ``2`` becomes ``two``.
|
|
|
|
* ``10`` becomes ``10``.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
You can pass in either an integer or a string representation of an integer.
|
|
|
|
|
2010-11-29 13:30:11 +08:00
|
|
|
.. templatefilter:: intcomma
|
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``intcomma``
|
|
|
|
============
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2016-12-29 18:53:55 +08:00
|
|
|
Converts an integer or float (or a string representation of either) to a string
|
|
|
|
containing commas every three digits.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* ``4500`` becomes ``4,500``.
|
2016-12-29 18:53:55 +08:00
|
|
|
* ``4500.2`` becomes ``4,500.2``.
|
2011-10-14 08:12:01 +08:00
|
|
|
* ``45000`` becomes ``45,000``.
|
|
|
|
* ``450000`` becomes ``450,000``.
|
|
|
|
* ``4500000`` becomes ``4,500,000``.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2016-01-03 18:56:22 +08:00
|
|
|
:doc:`/topics/i18n/formatting` will be respected if enabled,
|
2011-05-06 21:29:58 +08:00
|
|
|
e.g. with the ``'de'`` language:
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* ``45000`` becomes ``'45.000'``.
|
|
|
|
* ``450000`` becomes ``'450.000'``.
|
2011-05-06 21:29:58 +08:00
|
|
|
|
2010-11-29 13:30:11 +08:00
|
|
|
.. templatefilter:: intword
|
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``intword``
|
|
|
|
===========
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2016-12-29 18:53:55 +08:00
|
|
|
Converts a large integer (or a string representation of an integer) to a
|
2019-06-10 03:48:20 +08:00
|
|
|
friendly text representation. Translates ``1.0`` as a singular phrase and all
|
|
|
|
other numeric values as plural, this may be incorrect for some languages. Works
|
|
|
|
best for numbers over 1 million.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* ``1000000`` becomes ``1.0 million``.
|
|
|
|
* ``1200000`` becomes ``1.2 million``.
|
|
|
|
* ``1200000000`` becomes ``1.2 billion``.
|
2020-03-15 00:32:19 +08:00
|
|
|
* ``-1200000000`` becomes ``-1.2 billion``.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-09-24 00:45:40 +08:00
|
|
|
Values up to 10^100 (Googol) are supported.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2016-01-03 18:56:22 +08:00
|
|
|
:doc:`/topics/i18n/formatting` will be respected if enabled,
|
2011-05-06 21:29:58 +08:00
|
|
|
e.g. with the ``'de'`` language:
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* ``1000000`` becomes ``'1,0 Million'``.
|
2019-06-11 13:20:53 +08:00
|
|
|
* ``1200000`` becomes ``'1,2 Millionen'``.
|
2011-10-14 08:12:01 +08:00
|
|
|
* ``1200000000`` becomes ``'1,2 Milliarden'``.
|
2020-03-15 00:32:19 +08:00
|
|
|
* ``-1200000000`` becomes ``'-1,2 Milliarden'``.
|
2011-05-06 21:29:58 +08:00
|
|
|
|
2010-11-29 13:30:11 +08:00
|
|
|
.. templatefilter:: naturalday
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``naturalday``
|
|
|
|
==============
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
For dates that are the current day or within one day, return "today",
|
|
|
|
"tomorrow" or "yesterday", as appropriate. Otherwise, format the date using
|
|
|
|
the passed in format string.
|
|
|
|
|
2010-11-29 13:30:11 +08:00
|
|
|
**Argument:** Date formatting string as described in the :tfilter:`date` tag.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
Examples (when 'today' is 17 Feb 2007):
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* ``16 Feb 2007`` becomes ``yesterday``.
|
|
|
|
* ``17 Feb 2007`` becomes ``today``.
|
|
|
|
* ``18 Feb 2007`` becomes ``tomorrow``.
|
|
|
|
* Any other day is formatted according to given argument or the
|
|
|
|
:setting:`DATE_FORMAT` setting if no argument is given.
|
2010-11-29 13:30:11 +08:00
|
|
|
|
2011-04-22 20:02:47 +08:00
|
|
|
.. templatefilter:: naturaltime
|
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``naturaltime``
|
|
|
|
===============
|
2011-04-22 20:02:47 +08:00
|
|
|
|
2011-05-15 01:44:10 +08:00
|
|
|
For datetime values, returns a string representing how many seconds,
|
2012-12-27 04:47:29 +08:00
|
|
|
minutes or hours ago it was -- falling back to the :tfilter:`timesince`
|
2012-08-02 18:54:19 +08:00
|
|
|
format if the value is more than a day old. In case the datetime value is in
|
|
|
|
the future the return value will automatically use an appropriate phrase.
|
2011-04-22 20:02:47 +08:00
|
|
|
|
|
|
|
Examples (when 'now' is 17 Feb 2007 16:30:00):
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* ``17 Feb 2007 16:30:00`` becomes ``now``.
|
|
|
|
* ``17 Feb 2007 16:29:31`` becomes ``29 seconds ago``.
|
|
|
|
* ``17 Feb 2007 16:29:00`` becomes ``a minute ago``.
|
|
|
|
* ``17 Feb 2007 16:25:35`` becomes ``4 minutes ago``.
|
2014-09-06 07:55:24 +08:00
|
|
|
* ``17 Feb 2007 15:30:29`` becomes ``59 minutes ago``.
|
|
|
|
* ``17 Feb 2007 15:30:01`` becomes ``59 minutes ago``.
|
|
|
|
* ``17 Feb 2007 15:30:00`` becomes ``an hour ago``.
|
2011-10-14 08:12:01 +08:00
|
|
|
* ``17 Feb 2007 13:31:29`` becomes ``2 hours ago``.
|
2014-09-06 07:55:24 +08:00
|
|
|
* ``16 Feb 2007 13:31:29`` becomes ``1 day, 2 hours ago``.
|
|
|
|
* ``16 Feb 2007 13:30:01`` becomes ``1 day, 2 hours ago``.
|
|
|
|
* ``16 Feb 2007 13:30:00`` becomes ``1 day, 3 hours ago``.
|
|
|
|
* ``17 Feb 2007 16:30:30`` becomes ``30 seconds from now``.
|
|
|
|
* ``17 Feb 2007 16:30:29`` becomes ``29 seconds from now``.
|
2011-10-14 08:12:01 +08:00
|
|
|
* ``17 Feb 2007 16:31:00`` becomes ``a minute from now``.
|
|
|
|
* ``17 Feb 2007 16:34:35`` becomes ``4 minutes from now``.
|
2014-09-06 07:55:24 +08:00
|
|
|
* ``17 Feb 2007 17:30:29`` becomes ``an hour from now``.
|
2011-10-14 08:12:01 +08:00
|
|
|
* ``17 Feb 2007 18:31:29`` becomes ``2 hours from now``.
|
|
|
|
* ``18 Feb 2007 16:31:29`` becomes ``1 day from now``.
|
2012-08-02 18:54:19 +08:00
|
|
|
* ``26 Feb 2007 18:31:29`` becomes ``1 week, 2 days from now``.
|
2011-04-22 20:02:47 +08:00
|
|
|
|
2010-11-29 13:30:11 +08:00
|
|
|
.. templatefilter:: ordinal
|
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``ordinal``
|
|
|
|
===========
|
2010-11-29 13:30:11 +08:00
|
|
|
|
|
|
|
Converts an integer to its ordinal as a string.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* ``1`` becomes ``1st``.
|
|
|
|
* ``2`` becomes ``2nd``.
|
|
|
|
* ``3`` becomes ``3rd``.
|
2010-11-29 13:30:11 +08:00
|
|
|
|
|
|
|
You can pass in either an integer or a string representation of an integer.
|