``django.contrib.humanize``
Django Docs · نسخهٔ 33d2e5948a0b · BSD-3-Clause
===========================
`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 INSTALLED_APPS setting. Once you've done that, use {% load humanize %}` in a template, and you'll have access to the following filters.
`apnumber`
For numbers 1-9, returns the number spelled out. Otherwise, returns the number. This follows Associated Press style.
Examples:
- `
1becomesone`. - `
2becomestwo`. - `
10becomes10`.
You can pass in either an integer or a string representation of an integer.
`intcomma`
Converts an integer or float (or a string representation of either) to a string containing commas every three digits.
Examples:
- `
4500becomes4,500`. - `
4500.2becomes4,500.2`. - `
45000becomes45,000`. - `
450000becomes450,000`. - `
4500000becomes4,500,000`.
/topics/i18n/formatting will be respected if enabled, e.g. with the `'de'` language:
- `
45000becomes'45.000'`. - `
450000becomes'450.000'`.
`intword`
Converts a large integer (or a string representation of an integer) to a 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.
Examples:
- `
1000000becomes1.0 million`. - `
1200000becomes1.2 million`. - `
1200000000becomes1.2 billion`. - `
-1200000000becomes-1.2 billion`.
Values up to 10^100 (Googol) are supported.
/topics/i18n/formatting will be respected if enabled, e.g. with the `'de'` language:
- `
1000000becomes'1,0 Million'`. - `
1200000becomes'1,2 Millionen'`. - `
1200000000becomes'1,2 Milliarden'`. - `
-1200000000becomes'-1,2 Milliarden'`.
`naturalday`
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.
Argument: Date formatting string as described in the date tag.
Examples (when 'today' is 17 Feb 2007):
- `
16 Feb 2007becomesyesterday`. - `
17 Feb 2007becomestoday`. - `
18 Feb 2007becomestomorrow`. - Any other day is formatted according to given argument or the
DATE_FORMAT setting if no argument is given.
`naturaltime`
For datetime values, returns a string representing how many seconds, minutes or hours ago it was -- falling back to the timesince 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.
Examples (when 'now' is 17 Feb 2007 16:30:00):
- `
17 Feb 2007 16:30:00becomesnow`. - `
17 Feb 2007 16:29:31becomes29 seconds ago`. - `
17 Feb 2007 16:29:00becomesa minute ago`. - `
17 Feb 2007 16:25:35becomes4 minutes ago`. - `
17 Feb 2007 15:30:29becomes59 minutes ago`. - `
17 Feb 2007 15:30:01becomes59 minutes ago`. - `
17 Feb 2007 15:30:00becomesan hour ago`. - `
17 Feb 2007 13:31:29becomes2 hours ago`. - `
16 Feb 2007 13:31:29becomes1 day, 2 hours ago`. - `
16 Feb 2007 13:30:01becomes1 day, 2 hours ago`. - `
16 Feb 2007 13:30:00becomes1 day, 3 hours ago`. - `
17 Feb 2007 16:30:30becomes30 seconds from now`. - `
17 Feb 2007 16:30:29becomes29 seconds from now`. - `
17 Feb 2007 16:31:00becomesa minute from now`. - `
17 Feb 2007 16:34:35becomes4 minutes from now`. - `
17 Feb 2007 17:30:29becomesan hour from now`. - `
17 Feb 2007 18:31:29becomes2 hours from now`. - `
18 Feb 2007 16:31:29becomes1 day from now`. - `
26 Feb 2007 18:31:29becomes1 week, 2 days from now`.
`ordinal`
Converts an integer to its ordinal as a string.
Examples:
- `
1becomes1st`. - `
2becomes2nd`. - `
3becomes3rd`.
You can pass in either an integer or a string representation of an integer. Negative integers are returned unchanged.