How can I print or iterate over all the variables available in the context from the template code?
I know about {% debug %}
but it contains too much information. I'd just like to print variable names available in the current context.
Is there a way to do this without writing custom tag?
Use the Django debug toolbar - gives you this on the Templates tab, along a whole range of other useful debugging information.
If you use class-based views you can just give the current context like a variable to context:
class MainView(TemplateView):
template_name = 'base.html'
def get_context_data(self, **kwargs):
ctx = super(MainView, self).get_context_data(**kwargs)
ctx['ctx'] = ctx
return ctx
Than you can access the context with {{ctx}}
精彩评论