formatting.txt 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. ===================
  2. Format localization
  3. ===================
  4. Overview
  5. ========
  6. Django's formatting system is capable of displaying dates, times and numbers in
  7. templates using the format specified for the current
  8. :term:`locale <locale name>`. It also handles localized input in forms.
  9. Two users accessing the same content may see dates, times and numbers formatted
  10. in different ways, depending on the formats for their current locale.
  11. .. note::
  12. To enable number formatting with thousand separators, it is necessary to
  13. set :setting:`USE_THOUSAND_SEPARATOR = True <USE_THOUSAND_SEPARATOR>` in
  14. your settings file. Alternatively, you could use :tfilter:`intcomma` to
  15. format numbers in your template.
  16. .. note::
  17. There is a related :setting:`USE_I18N` setting that controls if Django
  18. should activate translation. See :doc:`/topics/i18n/translation` for more
  19. details.
  20. Locale aware input in forms
  21. ===========================
  22. When formatting is enabled, Django can use localized formats when parsing dates,
  23. times and numbers in forms. That means it tries different formats for different
  24. locales when guessing the format used by the user when inputting data on forms.
  25. .. note::
  26. Django uses different formats for displaying data to those it uses for
  27. parsing data. Most notably, the formats for parsing dates can't use the
  28. ``%a`` (abbreviated weekday name), ``%A`` (full weekday name),
  29. ``%b`` (abbreviated month name), ``%B`` (full month name),
  30. or ``%p`` (AM/PM).
  31. To enable a form field to localize input and output data use its ``localize``
  32. argument::
  33. class CashRegisterForm(forms.Form):
  34. product = forms.CharField()
  35. revenue = forms.DecimalField(max_digits=4, decimal_places=2, localize=True)
  36. .. _topic-l10n-templates:
  37. Controlling localization in templates
  38. =====================================
  39. Django tries to use a locale specific format whenever it outputs a value in a
  40. template.
  41. However, it may not always be appropriate to use localized values --
  42. for example, if you're outputting JavaScript or XML that is designed
  43. to be machine-readable, you will always want unlocalized values. You
  44. may also want to use localization in selected templates, rather than
  45. using localization everywhere.
  46. To allow for fine control over the use of localization, Django
  47. provides the ``l10n`` template library that contains the following
  48. tags and filters.
  49. Template tags
  50. -------------
  51. .. templatetag:: localize
  52. ``localize``
  53. ~~~~~~~~~~~~
  54. Enables or disables localization of template variables in the
  55. contained block.
  56. To activate or deactivate localization for a template block, use:
  57. .. code-block:: html+django
  58. {% load l10n %}
  59. {% localize on %}
  60. {{ value }}
  61. {% endlocalize %}
  62. {% localize off %}
  63. {{ value }}
  64. {% endlocalize %}
  65. When localization is disabled, the :ref:`localization settings <settings-l10n>`
  66. formats are applied.
  67. See :tfilter:`localize` and :tfilter:`unlocalize` for template filters that will
  68. do the same job on a per-variable basis.
  69. Template filters
  70. ----------------
  71. .. templatefilter:: localize
  72. ``localize``
  73. ~~~~~~~~~~~~
  74. Forces localization of a single value.
  75. For example:
  76. .. code-block:: html+django
  77. {% load l10n %}
  78. {{ value|localize }}
  79. To disable localization on a single value, use :tfilter:`unlocalize`. To control
  80. localization over a large section of a template, use the :ttag:`localize` template
  81. tag.
  82. .. templatefilter:: unlocalize
  83. ``unlocalize``
  84. ~~~~~~~~~~~~~~
  85. Forces a single value to be printed without localization.
  86. For example:
  87. .. code-block:: html+django
  88. {% load l10n %}
  89. {{ value|unlocalize }}
  90. To force localization of a single value, use :tfilter:`localize`. To
  91. control localization over a large section of a template, use the
  92. :ttag:`localize` template tag.
  93. Returns a string representation for numbers (``int``, ``float``, or
  94. ``Decimal``) with the :ref:`localization settings <settings-l10n>` formats
  95. applied.
  96. .. _custom-format-files:
  97. Creating custom format files
  98. ============================
  99. Django provides format definitions for many locales, but sometimes you might
  100. want to create your own, because a format file doesn't exist for your locale,
  101. or because you want to overwrite some of the values.
  102. To use custom formats, specify the path where you'll place format files
  103. first. To do that, set your :setting:`FORMAT_MODULE_PATH` setting to the
  104. package where format files will exist, for instance::
  105. FORMAT_MODULE_PATH = [
  106. "mysite.formats",
  107. "some_app.formats",
  108. ]
  109. Files are not placed directly in this directory, but in a directory named as
  110. the locale, and must be named ``formats.py``. Be careful not to put sensitive
  111. information in these files as values inside can be exposed if you pass the
  112. string to ``django.utils.formats.get_format()`` (used by the :tfilter:`date`
  113. template filter).
  114. To customize the English formats, a structure like this would be needed:
  115. .. code-block:: text
  116. mysite/
  117. formats/
  118. __init__.py
  119. en/
  120. __init__.py
  121. formats.py
  122. where :file:`formats.py` contains custom format definitions. For example::
  123. THOUSAND_SEPARATOR = "\xa0"
  124. to use a non-breaking space (Unicode ``00A0``) as a thousand separator,
  125. instead of the default for English, a comma.
  126. Limitations of the provided locale formats
  127. ==========================================
  128. Some locales use context-sensitive formats for numbers, which Django's
  129. localization system cannot handle automatically.
  130. Switzerland (German)
  131. --------------------
  132. The Swiss number formatting depends on the type of number that is being
  133. formatted. For monetary values, a comma is used as the thousand separator and
  134. a decimal point for the decimal separator. For all other numbers, a comma is
  135. used as decimal separator and a space as thousand separator. The locale format
  136. provided by Django uses the generic separators, a comma for decimal and a space
  137. for thousand separators.