开发者

How to print context content in the template?

开发者 https://www.devze.com 2022-12-15 04:50 出处:网络
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 name

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.

开发者_如何学C

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

0

精彩评论

暂无评论...
验证码 换一张
取 消