[Django] #27584: Template error raised in a parent template shows incorrect source location on debug page

42 views
Skip to first unread message

Django

unread,
Dec 9, 2016, 8:40:36 AM12/9/16
to django-...@googlegroups.com
#27584: Template error raised in a parent template shows incorrect source location
on debug page
-------------------------------------------+------------------------
Reporter: Bob | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 1.10
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------------+------------------------
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
{{{#!python
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.

--
Ticket URL: <https://code.djangoproject.com/ticket/27584>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Dec 9, 2016, 3:27:23 PM12/9/16
to django-...@googlegroups.com
#27584: Template error raised in a parent template shows incorrect source location
on debug page
---------------------------------+------------------------------------

Reporter: Bob | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by Tim Graham):

* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/27584#comment:1>

Django

unread,
Dec 13, 2016, 2:18:19 PM12/13/16
to django-...@googlegroups.com
#27584: Template error raised in a parent template shows incorrect source location
on debug page
---------------------------------+------------------------------------

Reporter: Bob | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by Tim Graham):

* cc: Preston Timmons (added)


Comment:

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

--
Ticket URL: <https://code.djangoproject.com/ticket/27584#comment:2>

Django

unread,
Dec 13, 2016, 9:39:53 PM12/13/16
to django-...@googlegroups.com
#27584: Template error raised in a parent template shows incorrect source location
on debug page
---------------------------------+------------------------------------

Reporter: Bob | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------

Comment (by Preston Timmons):

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

--
Ticket URL: <https://code.djangoproject.com/ticket/27584#comment:3>

Django

unread,
Dec 14, 2016, 7:27:52 AM12/14/16
to django-...@googlegroups.com
#27584: Template error raised in a parent template shows incorrect source location
on debug page
---------------------------------+------------------------------------

Reporter: Bob | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by Tim Graham):

* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/27584#comment:4>

Django

unread,
Dec 20, 2016, 10:54:27 AM12/20/16
to django-...@googlegroups.com
#27584: Template error raised in a parent template shows incorrect source location
on debug page
---------------------------------+------------------------------------
Reporter: Bob | Owner: nobody
Type: Bug | Status: closed

Component: Template system | Version: 1.10
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: new => closed
* resolution: => fixed


Comment:

In [changeset:"0b4d5172631af065601370a6d35d05b53eb2f5f6" 0b4d5172]:
{{{
#!CommitTicketReference repository=""
revision="0b4d5172631af065601370a6d35d05b53eb2f5f6"
Fixed #27584 -- Fixed display of render time template errors.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/27584#comment:5>

Django

unread,
Mar 20, 2017, 7:34:04 PM3/20/17
to django-...@googlegroups.com
#27584: Template error raised in a parent template shows incorrect source location
on debug page
---------------------------------+------------------------------------
Reporter: Bob | Owner: nobody
Type: Bug | Status: closed

Component: Template system | Version: 1.10
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------

Comment (by Tim Graham):

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

--
Ticket URL: <https://code.djangoproject.com/ticket/27584#comment:6>

Reply all
Reply to author
Forward
0 new messages