humanize.txt 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. ===========================
  2. ``django.contrib.humanize``
  3. ===========================
  4. .. module:: django.contrib.humanize
  5. :synopsis: A set of Django template filters useful for adding a "human
  6. touch" to data.
  7. A set of Django template filters useful for adding a "human touch" to data.
  8. To activate these filters, add ``'django.contrib.humanize'`` to your
  9. :setting:`INSTALLED_APPS` setting. Once you've done that, use
  10. ``{% load humanize %}`` in a template, and you'll have access to the following
  11. filters.
  12. .. templatefilter:: apnumber
  13. ``apnumber``
  14. ============
  15. For numbers 1-9, returns the number spelled out. Otherwise, returns the
  16. number. This follows Associated Press style.
  17. Examples:
  18. * ``1`` becomes ``one``.
  19. * ``2`` becomes ``two``.
  20. * ``10`` becomes ``10``.
  21. You can pass in either an integer or a string representation of an integer.
  22. .. templatefilter:: intcomma
  23. ``intcomma``
  24. ============
  25. Converts an integer or float (or a string representation of either) to a string
  26. containing commas every three digits.
  27. Examples:
  28. * ``4500`` becomes ``4,500``.
  29. * ``4500.2`` becomes ``4,500.2``.
  30. * ``45000`` becomes ``45,000``.
  31. * ``450000`` becomes ``450,000``.
  32. * ``4500000`` becomes ``4,500,000``.
  33. :doc:`/topics/i18n/formatting` will be respected if enabled,
  34. e.g. with the ``'de'`` language:
  35. * ``45000`` becomes ``'45.000'``.
  36. * ``450000`` becomes ``'450.000'``.
  37. .. templatefilter:: intword
  38. ``intword``
  39. ===========
  40. Converts a large integer (or a string representation of an integer) to a
  41. friendly text representation. Translates ``1.0`` as a singular phrase and all
  42. other numeric values as plural, this may be incorrect for some languages. Works
  43. best for numbers over 1 million.
  44. Examples:
  45. * ``1000000`` becomes ``1.0 million``.
  46. * ``1200000`` becomes ``1.2 million``.
  47. * ``1200000000`` becomes ``1.2 billion``.
  48. * ``-1200000000`` becomes ``-1.2 billion``.
  49. Values up to 10^100 (Googol) are supported.
  50. :doc:`/topics/i18n/formatting` will be respected if enabled,
  51. e.g. with the ``'de'`` language:
  52. * ``1000000`` becomes ``'1,0 Million'``.
  53. * ``1200000`` becomes ``'1,2 Millionen'``.
  54. * ``1200000000`` becomes ``'1,2 Milliarden'``.
  55. * ``-1200000000`` becomes ``'-1,2 Milliarden'``.
  56. .. versionchanged:: 3.0
  57. All numeric values are now translated as plural, except ``1.0`` which is
  58. translated as a singular phrase. This may be incorrect for some languages.
  59. .. versionchanged:: 3.1
  60. Support for negative integers was added.
  61. .. templatefilter:: naturalday
  62. ``naturalday``
  63. ==============
  64. For dates that are the current day or within one day, return "today",
  65. "tomorrow" or "yesterday", as appropriate. Otherwise, format the date using
  66. the passed in format string.
  67. **Argument:** Date formatting string as described in the :tfilter:`date` tag.
  68. Examples (when 'today' is 17 Feb 2007):
  69. * ``16 Feb 2007`` becomes ``yesterday``.
  70. * ``17 Feb 2007`` becomes ``today``.
  71. * ``18 Feb 2007`` becomes ``tomorrow``.
  72. * Any other day is formatted according to given argument or the
  73. :setting:`DATE_FORMAT` setting if no argument is given.
  74. .. templatefilter:: naturaltime
  75. ``naturaltime``
  76. ===============
  77. For datetime values, returns a string representing how many seconds,
  78. minutes or hours ago it was -- falling back to the :tfilter:`timesince`
  79. format if the value is more than a day old. In case the datetime value is in
  80. the future the return value will automatically use an appropriate phrase.
  81. Examples (when 'now' is 17 Feb 2007 16:30:00):
  82. * ``17 Feb 2007 16:30:00`` becomes ``now``.
  83. * ``17 Feb 2007 16:29:31`` becomes ``29 seconds ago``.
  84. * ``17 Feb 2007 16:29:00`` becomes ``a minute ago``.
  85. * ``17 Feb 2007 16:25:35`` becomes ``4 minutes ago``.
  86. * ``17 Feb 2007 15:30:29`` becomes ``59 minutes ago``.
  87. * ``17 Feb 2007 15:30:01`` becomes ``59 minutes ago``.
  88. * ``17 Feb 2007 15:30:00`` becomes ``an hour ago``.
  89. * ``17 Feb 2007 13:31:29`` becomes ``2 hours ago``.
  90. * ``16 Feb 2007 13:31:29`` becomes ``1 day, 2 hours ago``.
  91. * ``16 Feb 2007 13:30:01`` becomes ``1 day, 2 hours ago``.
  92. * ``16 Feb 2007 13:30:00`` becomes ``1 day, 3 hours ago``.
  93. * ``17 Feb 2007 16:30:30`` becomes ``30 seconds from now``.
  94. * ``17 Feb 2007 16:30:29`` becomes ``29 seconds from now``.
  95. * ``17 Feb 2007 16:31:00`` becomes ``a minute from now``.
  96. * ``17 Feb 2007 16:34:35`` becomes ``4 minutes from now``.
  97. * ``17 Feb 2007 17:30:29`` becomes ``an hour from now``.
  98. * ``17 Feb 2007 18:31:29`` becomes ``2 hours from now``.
  99. * ``18 Feb 2007 16:31:29`` becomes ``1 day from now``.
  100. * ``26 Feb 2007 18:31:29`` becomes ``1 week, 2 days from now``.
  101. .. templatefilter:: ordinal
  102. ``ordinal``
  103. ===========
  104. Converts an integer to its ordinal as a string.
  105. Examples:
  106. * ``1`` becomes ``1st``.
  107. * ``2`` becomes ``2nd``.
  108. * ``3`` becomes ``3rd``.
  109. You can pass in either an integer or a string representation of an integer.