diff --git a/debug_toolbar/panels/templates/panel.py b/debug_toolbar/panels/templates/panel.py index 72565f016..e9a27130f 100644 --- a/debug_toolbar/panels/templates/panel.py +++ b/debug_toolbar/panels/templates/panel.py @@ -96,10 +96,15 @@ def _store_template_info(self, sender, **kwargs): if pformatted is None: temp_layer = {} for key, value in context_layer.items(): + # Do not force evaluating LazyObject + if hasattr(value, "_wrapped"): + # SimpleLazyObject has __repr__ which includes actual value + # if it has been already evaluated + temp_layer[key] = repr(value) # Replace any request elements - they have a large # Unicode representation and the request data is # already made available from the Request panel. - if isinstance(value, http.HttpRequest): + elif isinstance(value, http.HttpRequest): temp_layer[key] = "<>" # Replace the debugging sql_queries element. The SQL # data is already made available from the SQL panel. diff --git a/docs/changes.rst b/docs/changes.rst index ad3cab34c..3640a6188 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -5,6 +5,7 @@ Pending ------- * Removed outdated third-party panels from the list. +* Avoid evaluating ``LazyObject`` in the templates panel. 4.2.0 (2023-08-10) ------------------ diff --git a/tests/panels/test_template.py b/tests/panels/test_template.py index 37e70cfa5..5ce28a880 100644 --- a/tests/panels/test_template.py +++ b/tests/panels/test_template.py @@ -2,6 +2,7 @@ from django.contrib.auth.models import User from django.template import Context, RequestContext, Template from django.test import override_settings +from django.utils.functional import SimpleLazyObject from ..base import BaseTestCase, IntegrationTestCase from ..forms import TemplateReprForm @@ -109,6 +110,14 @@ def test_empty_context(self): ["{'False': False, 'None': None, 'True': True}"], ) + def test_lazyobject(self): + response = self.panel.process_request(self.request) + t = Template("") + c = Context({"lazy": SimpleLazyObject(lambda: "lazy_value")}) + t.render(c) + self.assertNotIn("lazy_value", self.panel.content) + + @override_settings( DEBUG=True, DEBUG_TOOLBAR_PANELS=["debug_toolbar.panels.templates.TemplatesPanel"]