Skip to content

Commit

Permalink
WIP: instrument the jinja render function (more)
Browse files Browse the repository at this point in the history
It instruments the single template render, but not the inherited
templates and I'm guessing not the included templates either.
I suspect we're going to have to patch jinja templates more robustly
than relying on the django jinja backend template class.
  • Loading branch information
tim-schilling authored and matthiask committed Jun 27, 2024
1 parent 1f3a6dc commit d57985d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
22 changes: 22 additions & 0 deletions debug_toolbar/panels/templates/jinja2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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):
print(self.template.name)
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
2 changes: 2 additions & 0 deletions debug_toolbar/panels/templates/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down

0 comments on commit d57985d

Please sign in to comment.