Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/docs/ref/contrib/humanize.txt
blob: 8accfe326e6a9d6e30a92d6eaffd991dc09ffb6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
========================
django.contrib.humanize
========================

.. 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
``{% load humanize %}`` in a template, and you'll have access to the following
filters.

.. templatefilter:: apnumber

apnumber
--------

For numbers 1-9, returns the number spelled out. Otherwise, returns the
number. This follows Associated Press style.

Examples:

* ``1`` becomes ``one``.
* ``2`` becomes ``two``.
* ``10`` becomes ``10``.

You can pass in either an integer or a string representation of an integer.

.. templatefilter:: intcomma

intcomma
--------

Converts an integer to a string containing commas every three digits.

Examples:

* ``4500`` becomes ``4,500``.
* ``45000`` becomes ``45,000``.
* ``450000`` becomes ``450,000``.
* ``4500000`` becomes ``4,500,000``.

:ref:`Format localization <format-localization>` will be respected if enabled,
e.g. with the ``'de'`` language:

* ``45000`` becomes ``'45.000'``.
* ``450000`` becomes ``'450.000'``.

You can pass in either an integer or a string representation of an integer.

.. templatefilter:: intword

intword
-------

Converts a large integer to a friendly text representation. Works best for
numbers over 1 million.

Examples:

* ``1000000`` becomes ``1.0 million``.
* ``1200000`` becomes ``1.2 million``.
* ``1200000000`` becomes ``1.2 billion``.

Values up to 10^100 (Googol) are supported.

:ref:`Format localization <format-localization>` will be respected if enabled,
e.g. with the ``'de'`` language:

* ``1000000`` becomes ``'1,0 Million'``.
* ``1200000`` becomes ``'1,2 Million'``.
* ``1200000000`` becomes ``'1,2 Milliarden'``.

You can pass in either an integer or a string representation of an integer.

.. templatefilter:: naturalday

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 :tfilter:`date` tag.

Examples (when 'today' is 17 Feb 2007):

* ``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.

.. templatefilter:: naturaltime

naturaltime
-----------

For datetime values, returns a string representing how many seconds,
minutes or hours ago it was -- falling back to the :tfilter:`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: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``.
* ``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``.
* ``17 Feb 2007 13:31:29`` becomes ``2 hours ago``.
* ``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``.
* ``17 Feb 2007 16:31:00`` becomes ``a minute from now``.
* ``17 Feb 2007 16:34:35`` becomes ``4 minutes from now``.
* ``17 Feb 2007 17:30:29`` becomes ``an hour from now``.
* ``17 Feb 2007 18:31:29`` becomes ``2 hours from now``.
* ``18 Feb 2007 16:31:29`` becomes ``1 day from now``.
* ``26 Feb 2007 18:31:29`` becomes ``1 week, 2 days from now``.

.. templatefilter:: ordinal

ordinal
-------

Converts an integer to its ordinal as a string.

Examples:

* ``1`` becomes ``1st``.
* ``2`` becomes ``2nd``.
* ``3`` becomes ``3rd``.

You can pass in either an integer or a string representation of an integer.