Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#27584 closed Bug (fixed)

Template error raised in a parent template shows incorrect source location on debug page

Reported by: Bob Owned by: nobody
Component: Template system Version: 1.10
Severity: Normal Keywords:
Cc: Preston Timmons Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When an error is raised from a template the django debug page attempts to show the location in the template where the error occurred. When this error occurs in a tag located on a parent template the debug page incorrectly shows the source from the child template instead of the parent.

Using a simple instance of template inheritance:

base.html

{% load app_tags %}
<!DOCTYPE HTML>
<html>
<head>
    <title>basic</title>

	{% magic %}
		<div>banana</div>
	{% endmagic %}
</head>
<body>
    {% block fill %}
    {% endblock %}
</body>
</html>

specific.html

{% extends "base.html" %}

{% block fill %}

<div>
    something specific
</div>

{% endblock fill %}

And define magic tag to just error

from django import template

register = template.Library()

@register.tag(name="magic")
def do_magic(parser, token):
    nodelist = parser.parse(('endmagic',))
    parser.delete_first_token()
    return MagicNode()

class MagicNode(template.Node):
    def render(self, context):
            raise template.TemplateSyntaxError('oh dear')

With debug enabled loading a view that renders the specific.html template generates the debug page which displays

Error during template rendering

In template /home/bob/code/test-template-tag/testtemplatetag/basic/templates/specific.html, error at line 7
oh dear

And an excerpt from the specific.html template with line seven highlighted (the text "something specific"). Instead of the correct line from base.html.

I took a look through the history of the code generating the debug information, I believe it is this change https://github.com/django/django/commit/55f12f8709f0604df7e1817a4c114ead1fb9a311 that has changed the behaviour. Looking at the tags this was included in 1.9, so I checked out 1.8 and it correctly displays the base.html source code and identifies the magic tag as the line triggering the error.

Change History (6)

comment:1 by Tim Graham, 8 years ago

Triage Stage: UnreviewedAccepted

comment:2 by Tim Graham, 8 years ago

Cc: Preston Timmons added

I confirmed that 55f12f8709f0604df7e1817a4c114ead1fb9a311 is where the behavior changed. Preston, do you have any advise or pointers? #27593 may be related or a duplicate.

comment:3 by Preston Timmons, 8 years ago

I see the problem.

The debug information is populated correctly if the error happens during compilation time (when Template.compile_nodelist is called), but not at render time (when Node.render_annotated is called). This is due to this line:

https://github.com/django/django/blob/master/django/template/base.py#L963

The wrong source is used since context.template is always the parent template.

It needs some clean up, but here's an approach to fix the problem:

https://github.com/django/django/pull/7686

comment:4 by Tim Graham, 8 years ago

Has patch: set

comment:5 by Tim Graham <timograham@…>, 8 years ago

Resolution: fixed
Status: newclosed

In 0b4d5172:

Fixed #27584 -- Fixed display of render time template errors.

comment:6 by Tim Graham, 8 years ago

This ticket fixed {% include %} -- see #27956 for a similar issue with {% extends %}.

Note: See TracTickets for help on using tickets.
Back to Top