Loading django/template/context.py +3 −4 Original line number Diff line number Diff line Loading @@ -145,11 +145,10 @@ class RenderContext(BaseContext): return key in self.dicts[-1] def get(self, key, otherwise=None): d = self.dicts[-1] if key in d: return d[key] return otherwise return self.dicts[-1].get(key, otherwise) def __getitem__(self, key): return self.dicts[-1][key] # This is a function rather than module-level procedural code because we only # want it to execute if somebody uses RequestContext. Loading tests/template_tests/test_context.py +15 −0 Original line number Diff line number Diff line Loading @@ -3,6 +3,7 @@ from unittest import TestCase from django.template import Context, Variable, VariableDoesNotExist from django.template.context import RenderContext class ContextTests(TestCase): Loading Loading @@ -34,3 +35,17 @@ class ContextTests(TestCase): self.assertRaises(VariableDoesNotExist, Variable('new').resolve, empty_context) self.assertEqual(Variable('new').resolve(Context({'new': 'foo'})), 'foo') def test_render_context(self): test_context = RenderContext({'fruit': 'papaya'}) # Test that push() limits access to the topmost dict test_context.push() test_context['vegetable'] = 'artichoke' self.assertEqual(list(test_context), ['vegetable']) self.assertNotIn('fruit', test_context) with self.assertRaises(KeyError): test_context['fruit'] self.assertIsNone(test_context.get('fruit')) Loading
django/template/context.py +3 −4 Original line number Diff line number Diff line Loading @@ -145,11 +145,10 @@ class RenderContext(BaseContext): return key in self.dicts[-1] def get(self, key, otherwise=None): d = self.dicts[-1] if key in d: return d[key] return otherwise return self.dicts[-1].get(key, otherwise) def __getitem__(self, key): return self.dicts[-1][key] # This is a function rather than module-level procedural code because we only # want it to execute if somebody uses RequestContext. Loading
tests/template_tests/test_context.py +15 −0 Original line number Diff line number Diff line Loading @@ -3,6 +3,7 @@ from unittest import TestCase from django.template import Context, Variable, VariableDoesNotExist from django.template.context import RenderContext class ContextTests(TestCase): Loading Loading @@ -34,3 +35,17 @@ class ContextTests(TestCase): self.assertRaises(VariableDoesNotExist, Variable('new').resolve, empty_context) self.assertEqual(Variable('new').resolve(Context({'new': 'foo'})), 'foo') def test_render_context(self): test_context = RenderContext({'fruit': 'papaya'}) # Test that push() limits access to the topmost dict test_context.push() test_context['vegetable'] = 'artichoke' self.assertEqual(list(test_context), ['vegetable']) self.assertNotIn('fruit', test_context) with self.assertRaises(KeyError): test_context['fruit'] self.assertIsNone(test_context.get('fruit'))