出于调试目的,我希望在我的所有模板中都有一个变量,其中包含要呈现的模板的路径.例如,如果视图呈现templates/account/logout.html,我希望{{template_name}}包含字符串templates/account/logout.html.
我不想去改变任何观点(特别是因为我重复使用了很多应用程序),所以前进的方式似乎是一个内省处理器,可以反省一些内容.问题是要反省什么.
或者这可能是内置的,我不知道它?
简单的方法:
下载并使用django调试工具栏.你会得到你所追求的东西的近似值.
不太简单的方法:
替换Template.render
为django.test.utils.instrumented_test_render
,监听django.test.signals.template_rendered
信号,并将模板的名称添加到上下文中.请注意,TEMPLATE_DEBUG
您的设置文件中必须为true,否则将无法从中获取名称.
if settings.DEBUG and settings.TEMPLATE_DEBUG
from django.test.utils import instrumented_test_render
from django.test.signals import template_rendered
def add_template_name_to_context(self, sender, **kwargs)
template = kwargs['template']
if template.origin and template.origin.name
kwargs['context']['template_name'] = template.origin.name
Template.render = instrumented_test_render
template_rendered.connect(add_template_name_to_context)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3263 次 |
最近记录: |