1class KeepContext: 2 """ 3 Context manager that receives a `django.template.Context` instance and a list of keys 4 5 Once the context manager is exited, it removes `keys` from the context, to avoid 6 side effects in later layout objects that may use the same context variables. 7 8 Layout objects should use `extra_context` to introduce context variables, never 9 touch context object themselves, that could introduce side effects. 10 """ 11 12 def __init__(self, context, keys): 13 self.context = context 14 self.keys = keys 15 16 def __enter__(self): 17 pass 18 19 def __exit__(self, type, value, traceback): 20 for key in list(self.keys): 21 if key in self.context: 22 del self.context[key] 23