From acaa2015274fa79aa79184e1ee824cdc791f580e Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Thu, 4 Jun 2020 09:11:37 +0200 Subject: [PATCH] [3.1.x] Fixed #30134 -- Ensured unlocalized numbers are string representation in templates. Backport of 9e57b1efb5205bd94462e9de35254ec5ea6eb04e from master --- django/utils/formats.py | 2 ++ docs/releases/3.1.txt | 3 +++ docs/topics/i18n/formatting.txt | 3 +++ tests/i18n/tests.py | 1 + 4 files changed, 9 insertions(+) diff --git a/django/utils/formats.py b/django/utils/formats.py index d5e5a555f6..028e11415a 100644 --- a/django/utils/formats.py +++ b/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) diff --git a/docs/releases/3.1.txt b/docs/releases/3.1.txt index c489f9cd89..ba96ea670f 100644 --- a/docs/releases/3.1.txt +++ b/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 %} ` tag and :tfilter:`unlocalize` + filter no longer respect :setting:`DECIMAL_SEPARATOR` setting. + .. _deprecated-features-3.1: Features deprecated in 3.1 diff --git a/docs/topics/i18n/formatting.txt b/docs/topics/i18n/formatting.txt index 3dbb10f2dc..f58ff0e963 100644 --- a/docs/topics/i18n/formatting.txt +++ b/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 diff --git a/tests/i18n/tests.py b/tests/i18n/tests.py index 9b2d1258bc..affda21fb4 100644 --- a/tests/i18n/tests.py +++ b/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,