humanize.txt 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 to a string containing commas every three digits.
  26. Examples:
  27. * ``4500`` becomes ``4,500``.
  28. * ``45000`` becomes ``45,000``.
  29. * ``450000`` becomes ``450,000``.
  30. * ``4500000`` becomes ``4,500,000``.
  31. :ref:`Format localization <format-localization>` will be respected if enabled,
  32. e.g. with the ``'de'`` language:
  33. * ``45000`` becomes ``'45.000'``.
  34. * ``450000`` becomes ``'450.000'``.
  35. You can pass in either an integer or a string representation of an integer.
  36. .. templatefilter:: intword
  37. intword
  38. -------
  39. Converts a large integer to a friendly text representation. Works best for
  40. numbers over 1 million.
  41. Examples:
  42. * ``1000000`` becomes ``1.0 million``.
  43. * ``1200000`` becomes ``1.2 million``.
  44. * ``1200000000`` becomes ``1.2 billion``.
  45. Values up to 1000000000000000 (one quadrillion) are supported.
  46. :ref:`Format localization <format-localization>` will be respected if enabled,
  47. e.g. with the ``'de'`` language:
  48. * ``1000000`` becomes ``'1,0 Million'``.
  49. * ``1200000`` becomes ``'1,2 Million'``.
  50. * ``1200000000`` becomes ``'1,2 Milliarden'``.
  51. You can pass in either an integer or a string representation of an integer.
  52. .. templatefilter:: naturalday
  53. naturalday
  54. ----------
  55. For dates that are the current day or within one day, return "today",
  56. "tomorrow" or "yesterday", as appropriate. Otherwise, format the date using
  57. the passed in format string.
  58. **Argument:** Date formatting string as described in the :tfilter:`date` tag.
  59. Examples (when 'today' is 17 Feb 2007):
  60. * ``16 Feb 2007`` becomes ``yesterday``.
  61. * ``17 Feb 2007`` becomes ``today``.
  62. * ``18 Feb 2007`` becomes ``tomorrow``.
  63. * Any other day is formatted according to given argument or the
  64. :setting:`DATE_FORMAT` setting if no argument is given.
  65. .. templatefilter:: naturaltime
  66. naturaltime
  67. -----------
  68. .. versionadded:: 1.4
  69. For date and time values shows how many seconds, minutes or hours ago compared
  70. to current timestamp returns representing string. Otherwise, it behaves like
  71. :tfilter:`naturaldate`, so it can also take string argument for date formating.
  72. **Argument:** Date formatting string as described in the :tfilter:`date` tag.
  73. Examples (when 'now' is 17 Feb 2007 16:30:00):
  74. * ``17 Feb 2007 16:30:00`` becomes ``now``.
  75. * ``17 Feb 2007 16:29:31`` becomes ``29 seconds ago``.
  76. * ``17 Feb 2007 16:29:00`` becomes ``a minute ago``.
  77. * ``17 Feb 2007 16:25:35`` becomes ``4 minutes ago``.
  78. * ``17 Feb 2007 15:30:29`` becomes ``an hour ago``.
  79. * ``17 Feb 2007 13:31:29`` becomes ``2 hours ago``.
  80. * ``16 Feb 2007 13:31:29`` becomes ``yesterday``.
  81. * Any other day is formatted according to given argument or the
  82. :setting:`DATE_FORMAT` setting if no argument is given.
  83. .. templatefilter:: ordinal
  84. ordinal
  85. -------
  86. Converts an integer to its ordinal as a string.
  87. Examples:
  88. * ``1`` becomes ``1st``.
  89. * ``2`` becomes ``2nd``.
  90. * ``3`` becomes ``3rd``.
  91. You can pass in either an integer or a string representation of an integer.