diff --git a/debug_toolbar/panels/templates/jinja2.py b/debug_toolbar/panels/templates/jinja2.py new file mode 100644 index 000000000..d343cb140 --- /dev/null +++ b/debug_toolbar/panels/templates/jinja2.py @@ -0,0 +1,23 @@ +import functools + +from django.template.backends.jinja2 import Template as JinjaTemplate +from django.template.context import make_context +from django.test.signals import template_rendered + + +def patch_jinja_render(): + orig_render = JinjaTemplate.render + + @functools.wraps(orig_render) + def wrapped_render(self, context=None, request=None): + # This patching of render only instruments the rendering + # of the immediate template. It won't include the parent template(s). + self.name = self.template.name + template_rendered.send( + sender=self, template=self, context=make_context(context, request) + ) + return orig_render(self, context, request) + + if JinjaTemplate.render != wrapped_render: + JinjaTemplate.original_render = JinjaTemplate.render + JinjaTemplate.render = wrapped_render diff --git a/debug_toolbar/panels/templates/panel.py b/debug_toolbar/panels/templates/panel.py index 81d7e5fad..182f80aab 100644 --- a/debug_toolbar/panels/templates/panel.py +++ b/debug_toolbar/panels/templates/panel.py @@ -14,6 +14,7 @@ from debug_toolbar.panels import Panel from debug_toolbar.panels.sql.tracking import SQLQueryTriggered, allow_sql from debug_toolbar.panels.templates import views +from debug_toolbar.panels.templates.jinja2 import patch_jinja_render # Monkey-patch to enable the template_rendered signal. The receiver returns # immediately when the panel is disabled to keep the overhead small. @@ -25,6 +26,7 @@ Template.original_render = Template._render Template._render = instrumented_test_render +patch_jinja_render() # Monkey-patch to store items added by template context processors. The # overhead is sufficiently small to justify enabling it unconditionally. diff --git a/docs/changes.rst b/docs/changes.rst index 4d26be57f..6a876ad24 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -4,6 +4,10 @@ Change log Pending ------- +* Instrument the Django Jinja2 template backend. This only instruments + the immediate template that's rendered. It will not provide stats on + any parent templates. + 4.4.3 (2024-07-04) ------------------ diff --git a/example/settings.py b/example/settings.py index 1508b5a29..26b75fa5c 100644 --- a/example/settings.py +++ b/example/settings.py @@ -41,6 +41,12 @@ STATIC_URL = "/static/" TEMPLATES = [ + { + "NAME": "jinja2", + "BACKEND": "django.template.backends.jinja2.Jinja2", + "APP_DIRS": True, + "DIRS": [os.path.join(BASE_DIR, "example", "templates", "jinja2")], + }, { "BACKEND": "django.template.backends.django.DjangoTemplates", "APP_DIRS": True, @@ -54,7 +60,7 @@ "django.contrib.messages.context_processors.messages", ], }, - } + }, ] USE_TZ = True diff --git a/example/templates/index.html b/example/templates/index.html index 527f5d2a3..4b25aefca 100644 --- a/example/templates/index.html +++ b/example/templates/index.html @@ -9,6 +9,7 @@

Index of Tests

{% cache 10 index_cache %}