This class manages a stack of dictionaries to provide hierarchical variable lookups and scoping. It supports pushing and popping context levels, allowing users to retrieve values by searching through the stack from the most recent level upward. Additionally, it provides utilities for flattening the context into a single dictionary and managing variable assignments across different scope levels.
Attributes
| Attribute | Type | Description |
|---|
| dicts | list of dicts | A list of dictionaries representing the layered variable scopes, starting with a base of built-in constants. |
Constructor
Signature
def BaseContext(
dict_: dict or BaseContext = None
)
Parameters
| Name | Type | Description |
|---|
| dict_ | dict or BaseContext = None | An optional initial dictionary or another BaseContext instance to populate the context stack. |
Methods
push()
@classmethod
def push(
*args: dict or BaseContext,
**kwargs: any
) - > [ContextDict](contextdict.md?sid=django_template_context_contextdict)
Creates a new ContextDict scope by layering additional dictionaries or contexts onto the current stack.
Parameters
| Name | Type | Description |
|---|
| *args | dict or BaseContext | Positional arguments representing dictionaries or contexts to be added to the stack. |
| **kwargs | any | Key-value pairs to be included in the new context scope. |
Returns
| Type | Description |
|---|
[ContextDict](contextdict.md?sid=django_template_context_contextdict) | A new context object containing the combined scopes. |
pop()
@classmethod
def pop() - > dict
Removes and returns the most recent dictionary from the stack, raising ContextPopException if only the built-in layer remains.
Returns
| Type | Description |
|---|
dict | The dictionary that was removed from the top of the stack. |
set_upward()
@classmethod
def set_upward(
key: str,
value: any
) - > null
Set a variable in one of the higher contexts if it exists there, otherwise in the current context.
Parameters
| Name | Type | Description |
|---|
| key | str | The variable name to search for and update in the stack. |
| value | any | The value to assign to the found variable or the local context. |
Returns
get()
@classmethod
def get(
key: str,
otherwise: any = None
) - > any
Retrieves a variable's value from the stack, returning a default value if the key is not found.
Parameters
| Name | Type | Description |
|---|
| key | str | The variable name to look up. |
| otherwise | any = None | The fallback value to return if the key does not exist in any context layer. |
Returns
| Type | Description |
|---|
any | The value of the key if found, otherwise the provided default value. |
setdefault()
@classmethod
def setdefault(
key: str,
default: any = None
) - > any
Returns the value of a key if it exists; otherwise, sets the key to the default value in the current context and returns it.
Parameters
| Name | Type | Description |
|---|
| key | str | The variable name to retrieve or initialize. |
| default | any = None | The value to assign if the key is not found in the stack. |
Returns
| Type | Description |
|---|
any | The existing value or the newly set default value. |
new()
@classmethod
def new(
values: dict or BaseContext = None
) - > [BaseContext](basecontext.md?sid=django_template_context_basecontext)
Return a new context with the same properties, but with only the values given in 'values' stored.
Parameters
| Name | Type | Description |
|---|
| values | dict or BaseContext = None | The data to populate the new context's stack. |
Returns
| Type | Description |
|---|
[BaseContext](basecontext.md?sid=django_template_context_basecontext) | A new context instance sharing the same class and properties but with a reset dictionary stack. |
flatten()
@classmethod
def flatten() - > dict
Return self.dicts as one dictionary.
Returns
| Type | Description |
|---|
dict | A single dictionary containing all keys and values from the stack, with local values overriding global ones. |