class RequestContext
from django.template.context import RequestContext
This subclass of template.Context automatically populates itself using the processors defined in the engine's configuration. Additional processors can be specified as a list of callables using the "processors" keyword argument.
Ancestors (MRO)
- builtins.object
- django.template.context.BaseContext
- django.template.context.Context
- django.template.context.RequestContext
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 |
def __contains__(self, key)
django.template.context.BaseContext
def __contains__(self, key):
return any(key in d for d in self.dicts)
def __copy__(self)
django.template.context.Context
django.template.context.Context
def __copy__(self):
duplicate = super().__copy__()
duplicate.render_context = copy(self.render_context)
return duplicate
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.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, request, dict_=None, processors=None, use_l10n=None, use_tz=None, autoescape=True)
django.template.context.RequestContext
django.template.context.RequestContext
Initialize self. See help(type(self)) for accurate signature.
def __init__(
self,
request,
dict_=None,
processors=None,
use_l10n=None,
use_tz=None,
autoescape=True,
):
super().__init__(dict_, use_l10n=use_l10n, use_tz=use_tz, autoescape=autoescape)
self.request = request
self._processors = () if processors is None else tuple(processors)
self._processors_index = len(self.dicts)
# placeholder for context processors output
self.update({})
# empty dict for any new modifications
# (so that context processors don't overwrite them)
self.update({})
django.template.context.Context
Initialize self. See help(type(self)) for accurate signature.
def __init__(self, dict_=None, autoescape=True, use_l10n=None, use_tz=None):
self.autoescape = autoescape
self.use_l10n = use_l10n
self.use_tz = use_tz
self.template_name = "unknown"
self.render_context = RenderContext()
# Set to the original template -- as opposed to extended or included
# templates -- during rendering, see bind_template.
self.template = None
super().__init__(dict_)
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.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 bind_template(*args, **kwds)
django.template.context.RequestContext
django.template.context.RequestContext
@contextmanager
def bind_template(self, template):
if self.template is not None:
raise RuntimeError("Context is already bound to a template")
self.template = template
# Set context processors according to the template engine's settings.
processors = template.engine.template_context_processors + self._processors
updates = {}
for processor in processors:
context = processor(self.request)
try:
updates.update(context)
except TypeError as e:
raise TypeError(
f"Context processor {processor.__qualname__} didn't return a "
"dictionary."
) from e
self.dicts[self._processors_index] = updates
try:
yield
finally:
self.template = None
# Unset context processors.
self.dicts[self._processors_index] = {}
django.template.context.Context
@contextmanager
def bind_template(self, template):
if self.template is not None:
raise RuntimeError("Context is already bound to a template")
self.template = template
try:
yield
finally:
self.template = None
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.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.RequestContext
django.template.context.RequestContext
Return a new context with the same properties, but with only the values given in 'values' stored.
def new(self, values=None):
new_context = super().new(values)
# This is for backwards-compatibility: RequestContexts created via
# Context.new don't include values from context processors.
if hasattr(new_context, "_processors_index"):
del new_context._processors_index
return new_context
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 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
def update(self, other_dict)
django.template.context.Context
Push other_dict to the stack of dictionaries in the Context
def update(self, other_dict):
"Push other_dict to the stack of dictionaries in the Context"
if not hasattr(other_dict, "__getitem__"):
raise TypeError("other_dict must be a mapping (dictionary-like) object.")
if isinstance(other_dict, BaseContext):
other_dict = other_dict.dicts[1:].pop()
return ContextDict(self, other_dict)