formatting.txt 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. See :tfilter:`localize` and :tfilter:`unlocalize` for template filters that will
  66. do the same job on a per-variable basis.
  67. Template filters
  68. ----------------
  69. .. templatefilter:: localize
  70. ``localize``
  71. ~~~~~~~~~~~~
  72. Forces localization of a single value.
  73. For example:
  74. .. code-block:: html+django
  75. {% load l10n %}
  76. {{ value|localize }}
  77. To disable localization on a single value, use :tfilter:`unlocalize`. To control
  78. localization over a large section of a template, use the :ttag:`localize` template
  79. tag.
  80. .. templatefilter:: unlocalize
  81. ``unlocalize``
  82. ~~~~~~~~~~~~~~
  83. Forces a single value to be printed without localization.
  84. For example:
  85. .. code-block:: html+django
  86. {% load l10n %}
  87. {{ value|unlocalize }}
  88. To force localization of a single value, use :tfilter:`localize`. To
  89. control localization over a large section of a template, use the
  90. :ttag:`localize` template tag.
  91. Returns a string representation for unlocalized numbers (``int``, ``float``,
  92. or ``Decimal``).
  93. .. _custom-format-files:
  94. Creating custom format files
  95. ============================
  96. Django provides format definitions for many locales, but sometimes you might
  97. want to create your own, because a format file doesn't exist for your locale,
  98. or because you want to overwrite some of the values.
  99. To use custom formats, specify the path where you'll place format files
  100. first. To do that, set your :setting:`FORMAT_MODULE_PATH` setting to the
  101. package where format files will exist, for instance::
  102. FORMAT_MODULE_PATH = [
  103. "mysite.formats",
  104. "some_app.formats",
  105. ]
  106. Files are not placed directly in this directory, but in a directory named as
  107. the locale, and must be named ``formats.py``. Be careful not to put sensitive
  108. information in these files as values inside can be exposed if you pass the
  109. string to ``django.utils.formats.get_format()`` (used by the :tfilter:`date`
  110. template filter).
  111. To customize the English formats, a structure like this would be needed:
  112. .. code-block:: text
  113. mysite/
  114. formats/
  115. __init__.py
  116. en/
  117. __init__.py
  118. formats.py
  119. where :file:`formats.py` contains custom format definitions. For example::
  120. THOUSAND_SEPARATOR = "\xa0"
  121. to use a non-breaking space (Unicode ``00A0``) as a thousand separator,
  122. instead of the default for English, a comma.
  123. Limitations of the provided locale formats
  124. ==========================================
  125. Some locales use context-sensitive formats for numbers, which Django's
  126. localization system cannot handle automatically.
  127. Switzerland (German)
  128. --------------------
  129. The Swiss number formatting depends on the type of number that is being
  130. formatted. For monetary values, a comma is used as the thousand separator and
  131. a decimal point for the decimal separator. For all other numbers, a comma is
  132. used as decimal separator and a space as thousand separator. The locale format
  133. provided by Django uses the generic separators, a comma for decimal and a space
  134. for thousand separators.