kolypto
kolypto

Reputation: 35374

In Django templates, `Context` is a stack. What for?

Django docs say that Context object is a stack:

from django.template import Context
c = Context()
c['a'] = 1
c.push() # Make a new Context level
c['a'] = 2
print(repr(c)) # [{'a': 1}, {'a': 2}]

Also the docs say

Using a Context as a stack comes in handy in some custom template tags

However, there's no example for that. My suggestion: it's useful to render a subtemplate with a clean context ; but one can just use a new empty Context() object instead.

So, what's the use case?

Upvotes: 4

Views: 310

Answers (1)

Tobu
Tobu

Reputation: 25426

The use case would be scoping, for example in loops and template inheritance.

Upvotes: 1

Related Questions