humanize.txt 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. .. templatefilter:: naturalday
  57. ``naturalday``
  58. ==============
  59. For dates that are the current day or within one day, return "today",
  60. "tomorrow" or "yesterday", as appropriate. Otherwise, format the date using
  61. the passed in format string.
  62. **Argument:** Date formatting string as described in the :tfilter:`date` tag.
  63. Examples (when 'today' is 17 Feb 2007):
  64. * ``16 Feb 2007`` becomes ``yesterday``.
  65. * ``17 Feb 2007`` becomes ``today``.
  66. * ``18 Feb 2007`` becomes ``tomorrow``.
  67. * Any other day is formatted according to given argument or the
  68. :setting:`DATE_FORMAT` setting if no argument is given.
  69. .. templatefilter:: naturaltime
  70. ``naturaltime``
  71. ===============
  72. For datetime values, returns a string representing how many seconds,
  73. minutes or hours ago it was -- falling back to the :tfilter:`timesince`
  74. format if the value is more than a day old. In case the datetime value is in
  75. the future the return value will automatically use an appropriate phrase.
  76. Examples (when 'now' is 17 Feb 2007 16:30:00):
  77. * ``17 Feb 2007 16:30:00`` becomes ``now``.
  78. * ``17 Feb 2007 16:29:31`` becomes ``29 seconds ago``.
  79. * ``17 Feb 2007 16:29:00`` becomes ``a minute ago``.
  80. * ``17 Feb 2007 16:25:35`` becomes ``4 minutes ago``.
  81. * ``17 Feb 2007 15:30:29`` becomes ``59 minutes ago``.
  82. * ``17 Feb 2007 15:30:01`` becomes ``59 minutes ago``.
  83. * ``17 Feb 2007 15:30:00`` becomes ``an hour ago``.
  84. * ``17 Feb 2007 13:31:29`` becomes ``2 hours ago``.
  85. * ``16 Feb 2007 13:31:29`` becomes ``1 day, 2 hours ago``.
  86. * ``16 Feb 2007 13:30:01`` becomes ``1 day, 2 hours ago``.
  87. * ``16 Feb 2007 13:30:00`` becomes ``1 day, 3 hours ago``.
  88. * ``17 Feb 2007 16:30:30`` becomes ``30 seconds from now``.
  89. * ``17 Feb 2007 16:30:29`` becomes ``29 seconds from now``.
  90. * ``17 Feb 2007 16:31:00`` becomes ``a minute from now``.
  91. * ``17 Feb 2007 16:34:35`` becomes ``4 minutes from now``.
  92. * ``17 Feb 2007 17:30:29`` becomes ``an hour from now``.
  93. * ``17 Feb 2007 18:31:29`` becomes ``2 hours from now``.
  94. * ``18 Feb 2007 16:31:29`` becomes ``1 day from now``.
  95. * ``26 Feb 2007 18:31:29`` becomes ``1 week, 2 days from now``.
  96. .. templatefilter:: ordinal
  97. ``ordinal``
  98. ===========
  99. Converts an integer to its ordinal as a string.
  100. Examples:
  101. * ``1`` becomes ``1st``.
  102. * ``2`` becomes ``2nd``.
  103. * ``3`` becomes ``3rd``.
  104. You can pass in either an integer or a string representation of an integer.
  105. Negative integers are returned unchanged.