Skip to main content

Context

A stack container for variable context

Attributes

AttributeTypeDescription
autoescapeboolean = TrueBoolean flag that determines if HTML auto-escaping is enabled for variables within the template context.
use_l10nboolean = NoneFlag indicating whether localized formatting should be applied to data values within the context.
use_tzboolean = NoneFlag indicating whether timezone-aware datetime objects should be used during template rendering.
template_namestring = "unknown"The name of the template currently being processed, defaulting to "unknown" if not specified.
render_context[RenderContext](rendercontext.md?sid=django_template_context_rendercontext) = RenderContext()A specialized storage container for internal state and metadata used during the rendering process.
template[Template](../base/template.md?sid=django_template_base_template) = nullSet to the original template -- as opposed to extended or included templates -- during rendering, see bind_template.

Constructor

Signature

def Context(
dict_: dict = None,
autoescape: boolean = True,
use_l10n: boolean = None,
use_tz: boolean = None
) - > null

Parameters

NameTypeDescription
dict_dict = NoneAn optional dictionary to initialize the context stack.
autoescapeboolean = TrueDetermines if HTML autoescaping is enabled.
use_l10nboolean = NoneDetermines if localized formatting should be used.
use_tzboolean = NoneDetermines if timezone-aware dates should be used.

Methods


bind_template()

@classmethod
def bind_template(
template: [Template](../base/template.md?sid=django_template_base_template)
) - > contextmanager

Binds the context to a specific template instance during the rendering process and ensures it is cleared afterward. This context manager prevents re-binding if the context is already associated with a template.

Parameters

NameTypeDescription
template[Template](../base/template.md?sid=django_template_base_template)The Template object instance to associate with this context during rendering.

Returns

TypeDescription
contextmanagerA context manager that sets the template attribute for the duration of the block.

update()

@classmethod
def update(
other_dict: dict|BaseContext
) - > [ContextDict](contextdict.md?sid=django_template_context_contextdict)

Push other_dict to the stack of dictionaries in the Context

Parameters

NameTypeDescription
other_dict`dictBaseContext`

Returns

TypeDescription
[ContextDict](contextdict.md?sid=django_template_context_contextdict)A ContextDict object representing the new state of the stack after the push operation.