From the documentation, I understand that the context object is a stack. Then what are push()
and update()
doing with respect to this code segment? It is stated in the doc that push()
and update()
are similar but update()
takes a dictionary as argument. Then why are we using them simultaneously here?
import django.template
import django.template.loader
def render(name, *values):
ctx = django.template.Context()
for d in values:
ctx.push()
ctx.update(d)
t = django.template.loader.get_template(name)
return str(t.render(ctx))
Also, what is the need of having context object as a stack?
Edit : I went through the doc again and found the flatten()
function which flattens all the levels in the stack to make them comparable.
The only difference seems to be (also according to the tests) the way you pass arguments into the call. Update takes a dictionary while push takes keyword arguments.
And about the usefulness, the documentation says:
Using a Context as a stack comes in handy in some custom template tags.