2010-01-04 38 views
4

如何從模板代碼打印或遍歷上下文中可用的所有變量?如何在模板中打印上下文內容?

我知道{% debug %}但它包含了太多的信息。我只想打印當前上下文中可用的變量名稱。

有沒有辦法做到這一點,而不寫自定義標籤?

回答

8

使用Django debug toolbar - 在模板選項卡上爲您提供了其他一些有用的調試信息。

+0

斷開鏈接訪問上下文8月10日 – Alex 2012-08-10 02:46:43

+5

任何方式這樣做_without_修改應用程序範圍的配置? – Basic 2013-05-30 10:52:19

2

如果使用基於類的意見,您可以把當前上下文像變量上下文:

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 

比你可以用{{ctx}}