diff options
| author | George Y. Kussumoto <georgeyk.dev@gmail.com> | 2024-06-04 15:07:01 -0300 |
|---|---|---|
| committer | Sarah Boyce <42296566+sarahboyce@users.noreply.github.com> | 2024-06-14 14:07:51 +0200 |
| commit | 64443f555f362ddf0c9094f8531c3da72a753827 (patch) | |
| tree | bb6035bc9bc8d55dcb735398c6d137a081d2c3d7 | |
| parent | 49a3a8d9a22ebcb29fdc4bacaa204f0bd2a6abf6 (diff) | |
[5.1.x] Fixed #35417 -- Updated BaseContext.new() with values to create a context that can be flattened.
Backport of 2a32b233822683c51e59722b7c9aa0789fc4ab1b from main.
| -rw-r--r-- | AUTHORS | 1 | ||||
| -rw-r--r-- | django/template/context.py | 4 | ||||
| -rw-r--r-- | tests/template_tests/test_context.py | 11 |
3 files changed, 15 insertions, 1 deletions
@@ -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> 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 |
