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

This commit is contained in:
Claude Paroz 2020-06-04 09:11:37 +02:00 committed by Mariusz Felisiak
parent 51250d2f12
commit 9e57b1efb5
4 changed files with 9 additions and 0 deletions

View File

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

View File

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

View File

@ -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 control localization over a large section of a template, use the
:ttag:`localize` template tag. :ttag:`localize` template tag.
Returns a string representation for unlocalized numbers (``int``, ``float``,
or ``Decimal``).
.. _custom-format-files: .. _custom-format-files:
Creating custom format files Creating custom format files

View File

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