From 2a32b233822683c51e59722b7c9aa0789fc4ab1b Mon Sep 17 00:00:00 2001 From: "George Y. Kussumoto" Date: Tue, 4 Jun 2024 15:07:01 -0300 Subject: [PATCH] Fixed #35417 -- Updated BaseContext.new() with values to create a context that can be flattened. --- AUTHORS | 1 + django/template/context.py | 4 +++- tests/template_tests/test_context.py | 11 +++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index 56cda52202..bd86da5b57 100644 --- a/AUTHORS +++ b/AUTHORS @@ -375,6 +375,7 @@ answer newbie questions, and generally made Django that much better: George Karpenkov George Song George Vilches + George Y. Kussumoto Georg "Hugo" Bauer Georgi Stanojevski Gerardo Orozco diff --git a/django/template/context.py b/django/template/context.py index 080a2dd9c0..0c28b479cd 100644 --- a/django/template/context.py +++ b/django/template/context.py @@ -31,7 +31,9 @@ class BaseContext: def _reset_dicts(self, value=None): builtins = {"True": True, "False": False, "None": None} self.dicts = [builtins] - if value is not None: + if isinstance(value, BaseContext): + self.dicts += value.dicts[1:] + elif value is not None: self.dicts.append(value) def __copy__(self): diff --git a/tests/template_tests/test_context.py b/tests/template_tests/test_context.py index 7420bb4c36..6d8ee7a6e6 100644 --- a/tests/template_tests/test_context.py +++ b/tests/template_tests/test_context.py @@ -158,6 +158,17 @@ class ContextTests(SimpleTestCase): }, ) + def test_flatten_context_with_context_copy(self): + ctx1 = Context({"a": 2}) + ctx2 = ctx1.new(Context({"b": 4})) + self.assertEqual( + ctx2.dicts, [{"True": True, "False": False, "None": None}, {"b": 4}] + ) + self.assertEqual( + ctx2.flatten(), + {"False": False, "None": None, "True": True, "b": 4}, + ) + def test_context_comparable(self): """ #21765 -- equality comparison should work