Context
A stack container for variable context
Attributes
| Attribute | Type | Description |
|---|---|---|
| autoescape | boolean = True | Boolean flag that determines if HTML auto-escaping is enabled for variables within the template context. |
| use_l10n | boolean = None | Flag indicating whether localized formatting should be applied to data values within the context. |
| use_tz | boolean = None | Flag indicating whether timezone-aware datetime objects should be used during template rendering. |
| template_name | string = "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) = null | Set 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
| Name | Type | Description |
|---|---|---|
| dict_ | dict = None | An optional dictionary to initialize the context stack. |
| autoescape | boolean = True | Determines if HTML autoescaping is enabled. |
| use_l10n | boolean = None | Determines if localized formatting should be used. |
| use_tz | boolean = None | Determines 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
| Name | Type | Description |
|---|---|---|
| template | [Template](../base/template.md?sid=django_template_base_template) | The Template object instance to associate with this context during rendering. |
Returns
| Type | Description |
|---|---|
contextmanager | A 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
| Name | Type | Description |
|---|---|---|
| other_dict | `dict | BaseContext` |
Returns
| Type | Description |
|---|---|
[ContextDict](contextdict.md?sid=django_template_context_contextdict) | A ContextDict object representing the new state of the stack after the push operation. |