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.
* stage: Unreviewed => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/27584#comment:1>
* 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>
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>
* has_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/27584#comment:4>
* 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>
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>