Skip to main content

BaseContext

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

AttributeTypeDescription
dictslist of dictsA 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

NameTypeDescription
dict_dict or BaseContext = NoneAn 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

NameTypeDescription
*argsdict or BaseContextPositional arguments representing dictionaries or contexts to be added to the stack.
**kwargsanyKey-value pairs to be included in the new context scope.

Returns

TypeDescription
[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

TypeDescription
dictThe 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

NameTypeDescription
keystrThe variable name to search for and update in the stack.
valueanyThe value to assign to the found variable or the local context.

Returns

TypeDescription
null

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

NameTypeDescription
keystrThe variable name to look up.
otherwiseany = NoneThe fallback value to return if the key does not exist in any context layer.

Returns

TypeDescription
anyThe 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

NameTypeDescription
keystrThe variable name to retrieve or initialize.
defaultany = NoneThe value to assign if the key is not found in the stack.

Returns

TypeDescription
anyThe 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

NameTypeDescription
valuesdict or BaseContext = NoneThe data to populate the new context's stack.

Returns

TypeDescription
[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

TypeDescription
dictA single dictionary containing all keys and values from the stack, with local values overriding global ones.