From 292f372768836e2aebc713064c5139e8067eebcb Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Sat, 28 May 2022 11:52:06 +0200 Subject: [PATCH] Fixed #33748 -- Fixed date template filter crash with lazy format. Regression in 659d2421c7adbbcd205604002d521d82d6b0b465. --- django/utils/formats.py | 1 + tests/i18n/tests.py | 3 +++ tests/template_tests/filter_tests/test_date.py | 5 +++++ 3 files changed, 9 insertions(+) diff --git a/django/utils/formats.py b/django/utils/formats.py index 3f38322d84..b0a66e4e25 100644 --- a/django/utils/formats.py +++ b/django/utils/formats.py @@ -113,6 +113,7 @@ def get_format(format_type, lang=None, use_l10n=None): use_l10n = settings.USE_L10N if use_l10n and lang is None: lang = get_language() + format_type = str(format_type) # format_type may be lazy. cache_key = (format_type, lang) try: return _format_cache[cache_key] diff --git a/tests/i18n/tests.py b/tests/i18n/tests.py index 0093181f64..d79cdcd34d 100644 --- a/tests/i18n/tests.py +++ b/tests/i18n/tests.py @@ -1518,6 +1518,9 @@ class FormattingTests(SimpleTestCase): with translation.override("de", deactivate=True): self.assertEqual(".", get_format("DECIMAL_SEPARATOR", lang="en")) + def test_get_format_lazy_format(self): + self.assertEqual(get_format(gettext_lazy("DATE_FORMAT")), "N j, Y") + def test_localize_templatetag_and_filter(self): """ Test the {% localize %} templatetag and the localize/unlocalize filters. diff --git a/tests/template_tests/filter_tests/test_date.py b/tests/template_tests/filter_tests/test_date.py index a7c694d50e..b998f83ba6 100644 --- a/tests/template_tests/filter_tests/test_date.py +++ b/tests/template_tests/filter_tests/test_date.py @@ -72,6 +72,11 @@ class DateTests(TimezoneTestCase): output = self.engine.render_to_string("date09", {"t": time(0, 0)}) self.assertEqual(output, "00:00") + @setup({"datelazy": '{{ t|date:_("H:i") }}'}) + def test_date_lazy(self): + output = self.engine.render_to_string("datelazy", {"t": time(0, 0)}) + self.assertEqual(output, "00:00") + class FunctionTests(SimpleTestCase): def test_date(self):