Browse Source

Fixed #35417 -- Updated BaseContext.new() with values to create a context that can be flattened.

George Y. Kussumoto 9 months ago
parent
commit
2a32b23382
3 changed files with 15 additions and 1 deletions
  1. 1 0
      AUTHORS
  2. 3 1
      django/template/context.py
  3. 11 0
      tests/template_tests/test_context.py

+ 1 - 0
AUTHORS

@@ -375,6 +375,7 @@ answer newbie questions, and generally made Django that much better:
     George Karpenkov <george@metaworld.ru>
     George Song <george@damacy.net>
     George Vilches <gav@thataddress.com>
+    George Y. Kussumoto <georgeyk.dev@gmail.com>
     Georg "Hugo" Bauer <gb@hugo.westfalen.de>
     Georgi Stanojevski <glisha@gmail.com>
     Gerardo Orozco <gerardo.orozco.mosqueda@gmail.com>

+ 3 - 1
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):

+ 11 - 0
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