Browse Source

Fixed #30134 -- Ensured unlocalized numbers are string representation in templates.

Claude Paroz 4 years ago
parent
commit
9e57b1efb5
4 changed files with 9 additions and 0 deletions
  1. 2 0
      django/utils/formats.py
  2. 3 0
      docs/releases/3.1.txt
  3. 3 0
      docs/topics/i18n/formatting.txt
  4. 1 0
      tests/i18n/tests.py

+ 2 - 0
django/utils/formats.py

@@ -197,6 +197,8 @@ def localize(value, use_l10n=None):
     elif isinstance(value, bool):  # Make sure booleans don't get treated as numbers
         return str(value)
     elif isinstance(value, (decimal.Decimal, float, int)):
+        if use_l10n is False:
+            return str(value)
         return number_format(value, use_l10n=use_l10n)
     elif isinstance(value, datetime.datetime):
         return date_format(value, 'DATETIME_FORMAT', use_l10n=use_l10n)

+ 3 - 0
docs/releases/3.1.txt

@@ -677,6 +677,9 @@ Miscellaneous
 * The undocumented ``django.contrib.postgres.fields.jsonb.JsonAdapter`` class
   is removed.
 
+* The :ttag:`{% localize off %} <localize>` tag and :tfilter:`unlocalize`
+  filter no longer respect :setting:`DECIMAL_SEPARATOR` setting.
+
 .. _deprecated-features-3.1:
 
 Features deprecated in 3.1

+ 3 - 0
docs/topics/i18n/formatting.txt

@@ -142,6 +142,9 @@ To force localization of a single value, use :tfilter:`localize`. To
 control localization over a large section of a template, use the
 :ttag:`localize` template tag.
 
+Returns a string representation for unlocalized numbers  (``int``, ``float``,
+or ``Decimal``).
+
 .. _custom-format-files:
 
 Creating custom format files

+ 1 - 0
tests/i18n/tests.py

@@ -1218,6 +1218,7 @@ class FormattingTests(SimpleTestCase):
         for use_l10n in [True, False]:
             with self.subTest(use_l10n=use_l10n), self.settings(
                 USE_L10N=use_l10n,
+                DECIMAL_SEPARATOR=',',
                 USE_THOUSAND_SEPARATOR=True,
                 THOUSAND_SEPARATOR='°',
                 NUMBER_GROUPING=2,