class RenderContext
from django.template.context import RenderContext
A stack container for storing Template state. RenderContext simplifies the implementation of template Nodes by providing a safe place to store state between invocations of a node's `render` method. The RenderContext also provides scoping rules that are more sensible for 'template local' variables. The render context stack is pushed before each template is rendered, creating a fresh scope with nothing in it. Name resolution fails if a variable is not found at the top of the RequestContext stack. Thus, variables are local to a specific template and don't affect the rendering of other templates as they would if they were stored in the normal template context.
Ancestors (MRO)
- builtins.object
- django.template.context.BaseContext
- django.template.context.RenderContext
Attribute | Type | Defined in |
---|---|---|
__dict__ |
getset_descriptor
|
django.template.context.BaseContext |
__weakref__ |
getset_descriptor
|
django.template.context.BaseContext |
Attribute | Value | Defined in |
---|---|---|
__hash__ |
None |
django.template.context.BaseContext |
template |
None |
django.template.context.RenderContext |
def __contains__(self, key)
django.template.context.RenderContext
django.template.context.RenderContext
def __contains__(self, key):
return key in self.dicts[-1]
django.template.context.BaseContext
def __contains__(self, key):
return any(key in d for d in self.dicts)
def __copy__(self)
django.template.context.BaseContext
def __copy__(self):
duplicate = copy(super())
duplicate.dicts = self.dicts[:]
return duplicate
def __delitem__(self, key)
django.template.context.BaseContext
Delete a variable from the current context
def __delitem__(self, key):
"Delete a variable from the current context"
del self.dicts[-1][key]
def __eq__(self, other)
django.template.context.BaseContext
Compare two contexts by comparing theirs 'dicts' attributes.
def __eq__(self, other):
"""
Compare two contexts by comparing theirs 'dicts' attributes.
"""
if not isinstance(other, BaseContext):
return NotImplemented
# flatten dictionaries because they can be put in a different order.
return self.flatten() == other.flatten()
def __getitem__(self, key)
django.template.context.RenderContext
django.template.context.RenderContext
Get a variable's value, starting at the current context and going upward
def __getitem__(self, key):
return self.dicts[-1][key]
django.template.context.BaseContext
Get a variable's value, starting at the current context and going upward
def __getitem__(self, key):
"Get a variable's value, starting at the current context and going upward"
for d in reversed(self.dicts):
if key in d:
return d[key]
raise KeyError(key)
def __init__(self, dict_=None)
django.template.context.BaseContext
Initialize self. See help(type(self)) for accurate signature.
def __init__(self, dict_=None):
self._reset_dicts(dict_)
def __iter__(self)
django.template.context.RenderContext
django.template.context.RenderContext
def __iter__(self):
yield from self.dicts[-1]
django.template.context.BaseContext
def __iter__(self):
return reversed(self.dicts)
def __repr__(self)
django.template.context.BaseContext
Return repr(self).
def __repr__(self):
return repr(self.dicts)
def __setitem__(self, key, value)
django.template.context.BaseContext
Set a variable in the current context
def __setitem__(self, key, value):
"Set a variable in the current context"
self.dicts[-1][key] = value
def flatten(self)
django.template.context.BaseContext
Return self.dicts as one dictionary.
def flatten(self):
"""
Return self.dicts as one dictionary.
"""
flat = {}
for d in self.dicts:
flat.update(d)
return flat
def get(self, key, otherwise=None)
django.template.context.RenderContext
django.template.context.RenderContext
def get(self, key, otherwise=None):
return self.dicts[-1].get(key, otherwise)
django.template.context.BaseContext
def get(self, key, otherwise=None):
for d in reversed(self.dicts):
if key in d:
return d[key]
return otherwise
def new(self, values=None)
django.template.context.BaseContext
Return a new context with the same properties, but with only the values given in 'values' stored.
def new(self, values=None):
"""
Return a new context with the same properties, but with only the
values given in 'values' stored.
"""
new_context = copy(self)
new_context._reset_dicts(values)
return new_context
def pop(self)
django.template.context.BaseContext
def pop(self):
if len(self.dicts) == 1:
raise ContextPopException
return self.dicts.pop()
def push(self, *args, **kwargs)
django.template.context.BaseContext
def push(self, *args, **kwargs):
dicts = []
for d in args:
if isinstance(d, BaseContext):
dicts += d.dicts[1:]
else:
dicts.append(d)
return ContextDict(self, *dicts, **kwargs)
def push_state(*args, **kwds)
django.template.context.RenderContext
@contextmanager
def push_state(self, template, isolated_context=True):
initial = self.template
self.template = template
if isolated_context:
self.push()
try:
yield
finally:
self.template = initial
if isolated_context:
self.pop()
def set_upward(self, key, value)
django.template.context.BaseContext
Set a variable in one of the higher contexts if it exists there, otherwise in the current context.
def set_upward(self, key, value):
"""
Set a variable in one of the higher contexts if it exists there,
otherwise in the current context.
"""
context = self.dicts[-1]
for d in reversed(self.dicts):
if key in d:
context = d
break
context[key] = value
def setdefault(self, key, default=None)
django.template.context.BaseContext
def setdefault(self, key, default=None):
try:
return self[key]
except KeyError:
self[key] = default
return default