formatting.txt 6.2 KB

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