Skip to content

Commit

Permalink
Fixed #35417 -- Updated BaseContext.new() with values to create a con…
Browse files Browse the repository at this point in the history
…text that can be flattened.
  • Loading branch information
georgeyk authored and sarahboyce committed Jun 13, 2024
1 parent 8733e9a commit 2a32b23
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ answer newbie questions, and generally made Django that much better:
George Karpenkov <[email protected]>
George Song <[email protected]>
George Vilches <[email protected]>
George Y. Kussumoto <[email protected]>
Georg "Hugo" Bauer <[email protected]>
Georgi Stanojevski <[email protected]>
Gerardo Orozco <[email protected]>
Expand Down
4 changes: 3 additions & 1 deletion django/template/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ def __init__(self, dict_=None):
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):
Expand Down
11 changes: 11 additions & 0 deletions tests/template_tests/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,17 @@ def test_flatten_context_with_context(self):
},
)

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
Expand Down

0 comments on commit 2a32b23

Please sign in to comment.