Kugel
Kugel

Reputation: 19814

How to print context content in the template?

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?

Upvotes: 8

Views: 14619

Answers (2)

Thorin Schiffer
Thorin Schiffer

Reputation: 2846

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}}

Upvotes: 7

Daniel Roseman
Daniel Roseman

Reputation: 599580

Use the Django debug toolbar - gives you this on the Templates tab, along a whole range of other useful debugging information.

Upvotes: 9

Related Questions