From 2913c39fb418920919ef1634cd6979879ceb4e35 Mon Sep 17 00:00:00 2001 From: Allan Simon Date: Tue, 6 Aug 2019 10:35:06 +0200 Subject: [PATCH 1/3] fix #902, correctly display the stacktrace for SQL requests in template --- debug_toolbar/utils.py | 54 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/debug_toolbar/utils.py b/debug_toolbar/utils.py index 0092de98e..ebb837176 100644 --- a/debug_toolbar/utils.py +++ b/debug_toolbar/utils.py @@ -127,13 +127,59 @@ 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) - line = exception_info["line"] - source_lines = exception_info["source_lines"] - name = exception_info["name"] + # If the node is in the top level template, use the original source lookup code + if context.template.origin == node.origin: + source_info = context.template.get_exception_info(Exception("DDT"), node.token) + else: # otherwise, its an included template so load from the node's origin + source_info = get_template_source_from_node(node) + line = source_info["line"] + source_lines = source_info["source_lines"] + name = source_info["name"] return line, source_lines, name +def get_template_source_from_node(node): + """Very similar to django.template.base.get_exception_info + Instead of using the Templates source, which does not include + the source from included files, this method loads the source + from the file referenced by node.origin. + """ + source = node.origin.loader.get_contents(node.origin) + start, end = node.token.position + context_lines = 10 + line = 0 + upto = 0 + source_lines = [] + before = during = after = "" + for num, next in enumerate(linebreak_iter(source)): + if start >= upto and end <= next: + line = num + before = escape(source[upto:start]) + during = escape(source[start:end]) + after = escape(source[end:next]) + source_lines.append((num, escape(source[upto:next]))) + upto = next + total = len(source_lines) + + top = max(1, line - context_lines) + bottom = min(total, line + 1 + context_lines) + + return { + "source_lines": source_lines[top:bottom], + "line": line, + "name": node.origin.name, + } + + +def linebreak_iter(template_source): + yield 0 + p = template_source.find("\n") + while p >= 0: + yield p + 1 + p = template_source.find("\n", p + 1) + yield len(template_source) + 1 + + def get_name_from_obj(obj): if hasattr(obj, "__name__"): name = obj.__name__ From 456c13e21d3d22604a0756ca1ab1e4f343e62f2e Mon Sep 17 00:00:00 2001 From: Allan Simon Date: Tue, 6 Aug 2019 11:26:02 +0200 Subject: [PATCH 2/3] Update utils.py --- debug_toolbar/utils.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/debug_toolbar/utils.py b/debug_toolbar/utils.py index ebb837176..dd7567a5f 100644 --- a/debug_toolbar/utils.py +++ b/debug_toolbar/utils.py @@ -154,9 +154,6 @@ def get_template_source_from_node(node): for num, next in enumerate(linebreak_iter(source)): if start >= upto and end <= next: line = num - before = escape(source[upto:start]) - during = escape(source[start:end]) - after = escape(source[end:next]) source_lines.append((num, escape(source[upto:next]))) upto = next total = len(source_lines) From 3ff12b6511f9e1cfcd1fb449eb56750f596ad102 Mon Sep 17 00:00:00 2001 From: Allan Simon Date: Tue, 6 Aug 2019 14:57:38 +0200 Subject: [PATCH 3/3] Update utils.py --- debug_toolbar/utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/debug_toolbar/utils.py b/debug_toolbar/utils.py index dd7567a5f..0ed800b63 100644 --- a/debug_toolbar/utils.py +++ b/debug_toolbar/utils.py @@ -150,7 +150,6 @@ def get_template_source_from_node(node): line = 0 upto = 0 source_lines = [] - before = during = after = "" for num, next in enumerate(linebreak_iter(source)): if start >= upto and end <= next: line = num