Skip to content

Show template context on included templates #1436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion debug_toolbar/panels/history/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


class HistoryPanel(Panel):
""" A panel to display History """
"""A panel to display History"""

title = _("History")
nav_title = _("History")
Expand Down
9 changes: 8 additions & 1 deletion debug_toolbar/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,14 @@ def get_template_context(node, context, context_lines=3):


def get_template_source_from_exception_info(node, context):
exception_info = context.template.get_exception_info(Exception("DDT"), node.token)
if context.template.origin == node.origin:
exception_info = context.template.get_exception_info(
Exception("DDT"), node.token
)
else:
exception_info = context.render_context.template.get_exception_info(
Exception("DDT"), node.token
)
line = exception_info["line"]
source_lines = exception_info["source_lines"]
name = exception_info["name"]
Expand Down
45 changes: 45 additions & 0 deletions tests/panels/test_sql.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
import os
import unittest

import django
Expand Down Expand Up @@ -396,3 +397,47 @@ def test_prettify_sql(self):
self.panel.generate_stats(self.request, response)
self.assertEqual(len(self.panel._queries), 1)
self.assertEqual(pretty_sql, self.panel._queries[-1][1]["sql"])

@override_settings(
DEBUG=True,
)
def test_flat_template_information(self):
"""
Test case for when the query is used in a flat template hierarchy
(without included templates).
"""
self.assertEqual(len(self.panel._queries), 0)

users = User.objects.all()
render(self.request, "sql/flat.html", {"users": users})

self.assertEqual(len(self.panel._queries), 1)

query = self.panel._queries[0]
template_info = query[1]["template_info"]
template_name = os.path.basename(template_info["name"])
self.assertEqual(template_name, "flat.html")
self.assertEqual(template_info["context"][2]["content"].strip(), "{{ users }}")
self.assertEqual(template_info["context"][2]["highlight"], True)

@override_settings(
DEBUG=True,
)
def test_nested_template_information(self):
"""
Test case for when the query is used in a nested template
hierarchy (with included templates).
"""
self.assertEqual(len(self.panel._queries), 0)

users = User.objects.all()
render(self.request, "sql/nested.html", {"users": users})

self.assertEqual(len(self.panel._queries), 1)

query = self.panel._queries[0]
template_info = query[1]["template_info"]
template_name = os.path.basename(template_info["name"])
self.assertEqual(template_name, "included.html")
self.assertEqual(template_info["context"][0]["content"].strip(), "{{ users }}")
self.assertEqual(template_info["context"][0]["highlight"], True)
4 changes: 4 additions & 0 deletions tests/templates/sql/flat.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{% extends "base.html" %}
{% block content %}
{{ users }}
{% endblock %}
1 change: 1 addition & 0 deletions tests/templates/sql/included.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ users }}
4 changes: 4 additions & 0 deletions tests/templates/sql/nested.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{% extends "base.html" %}
{% block content %}
{% include "sql/included.html" %}
{% endblock %}