class IncludeNode
from django.template.loader_tags import IncludeNode
Ancestors (MRO)
- builtins.object
- django.template.base.Node
- django.template.loader_tags.IncludeNode
Attribute | Type | Defined in |
---|---|---|
__dict__ |
getset_descriptor
|
django.template.base.Node |
__weakref__ |
getset_descriptor
|
django.template.base.Node |
Attribute | Value | Defined in |
---|---|---|
child_nodelists |
('nodelist',) |
django.template.base.Node |
context_key |
__include_context |
django.template.loader_tags.IncludeNode |
must_be_first |
False |
django.template.base.Node |
token |
None |
django.template.base.Node |
def __init__(self)
django.template.loader_tags.IncludeNode
Initialize self. See help(type(self)) for accurate signature.
def __init__(
self, template, *args, extra_context=None, isolated_context=False, **kwargs
):
self.template = template
self.extra_context = extra_context or {}
self.isolated_context = isolated_context
super().__init__(*args, **kwargs)
def __repr__(self)
django.template.loader_tags.IncludeNode
Return repr(self).
def __repr__(self):
return f"<{self.__class__.__qualname__}: template={self.template!r}>"
def get_nodes_by_type(self, nodetype)
django.template.base.Node
Return a list of all nodes (within this node and its nodelist) of the given type
def get_nodes_by_type(self, nodetype):
"""
Return a list of all nodes (within this node and its nodelist)
of the given type
"""
nodes = []
if isinstance(self, nodetype):
nodes.append(self)
for attr in self.child_nodelists:
nodelist = getattr(self, attr, None)
if nodelist:
nodes.extend(nodelist.get_nodes_by_type(nodetype))
return nodes
def render(self, context)
django.template.loader_tags.IncludeNode
django.template.loader_tags.IncludeNode
Render the specified template and context. Cache the template object in render_context to avoid reparsing and loading when used in a for loop.
def render(self, context):
"""
Render the specified template and context. Cache the template object
in render_context to avoid reparsing and loading when used in a for
loop.
"""
template = self.template.resolve(context)
# Does this quack like a Template?
if not callable(getattr(template, "render", None)):
# If not, try the cache and select_template().
template_name = template or ()
if isinstance(template_name, str):
template_name = (
construct_relative_path(
self.origin.template_name,
template_name,
),
)
else:
template_name = tuple(template_name)
cache = context.render_context.dicts[0].setdefault(self, {})
template = cache.get(template_name)
if template is None:
template = context.template.engine.select_template(template_name)
cache[template_name] = template
# Use the base.Template of a backends.django.Template.
elif hasattr(template, "template"):
template = template.template
values = {
name: var.resolve(context) for name, var in self.extra_context.items()
}
if self.isolated_context:
return template.render(context.new(values))
with context.push(**values):
return template.render(context)
django.template.base.Node
Return the node rendered as a string.
def render(self, context):
"""
Return the node rendered as a string.
"""
pass
def render_annotated(self, context)
django.template.base.Node
Render the node. If debug is True and an exception occurs during rendering, the exception is annotated with contextual line information where it occurred in the template. For internal usage this method is preferred over using the render method directly.
def render_annotated(self, context):
"""
Render the node. If debug is True and an exception occurs during
rendering, the exception is annotated with contextual line information
where it occurred in the template. For internal usage this method is
preferred over using the render method directly.
"""
try:
return self.render(context)
except Exception as e:
if context.template.engine.debug:
# Store the actual node that caused the exception.
if not hasattr(e, "_culprit_node"):
e._culprit_node = self
if (
not hasattr(e, "template_debug")
and context.render_context.template.origin == e._culprit_node.origin
):
e.template_debug = (
context.render_context.template.get_exception_info(
e,
e._culprit_node.token,
)
)
raise