Opened 6 years ago

Closed 3 years ago

#28935 closed Bug (fixed)

Template error raised in an {% extends %} child template shows incorrect source location on debug page

Reported by: Matt Westcott Owned by: cammil
Component: Template system Version: dev
Severity: Normal Keywords:
Cc: Preston Timmons, Collin Anderson, pascal chambon, Simone Lazzaris, Min ho Kim, cammil Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

If an error occurs in the processing of a template tag on a template that extends another via {% extends %}, the "Error during template rendering" section of the debug page incorrectly shows an excerpt from the parent template rather than the child.

To reproduce:

base.html:

{% block body %}{% endblock %}

home.html:

{% extends "base.html" %}

{% block body %}
    {% include "does_not_exist" %}
{% endblock %}

views.py:

from django.shortcuts import render

def home(request):
    return render(request, 'home.html')

The resulting error page displays:

Error during template rendering
In template /Users/matthew/Development/tbx/wagtail/debug/brokendebug/core/templates/base.html, error at line 0

does_not_exist
1	{% block body %}{% endblock %}
2	

instead of the expected error output:

Error during template rendering
In template /Users/matthew/Development/tbx/wagtail/debug/brokendebug/core/templates/home.html, error at line 4

does_not_exist
1	{% extends "base.html" %}
2	
3	{% block body %}
4	    {% include "does_not_exist" %}
5	{% endblock %}
6	

Bisecting shows that this bug was introduced in #27956 (e643ba8bcf0b1da517cbab689ac157ee031202a3), which was a fix for the opposite case: an error occurring on the parent template. (In the above test case, moving the line {% include "does_not_exist" %} to base.html turns it into a test case for #27956, and fails on pre-e643ba8bcf0b1da517cbab689ac157ee031202a3 revisions as expected.)

Change History (24)

comment:1 by Tim Graham, 6 years ago

Cc: Preston Timmons added
Component: UncategorizedTemplate system
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

comment:4 by Collin Anderson, 6 years ago

Cc: Collin Anderson added

comment:5 by Ankur Jain, 6 years ago

Owner: set to Ankur Jain
Status: newassigned

comment:6 by Ankur Jain, 6 years ago

When the parser runs then it appends all the child template nodes to the parent template nodes and hence when template not found error comes then it's reported from the parent html instead of the child html.

comment:7 by Simon Charette, 6 years ago

#29740 was a duplicate.

in reply to:  6 comment:8 by Michael F. Lamb, 5 years ago

Replying to Ankur Jain:
Hi, I'm interested in working on this bug. Are you still working on it, Ankur?

comment:9 by pascal chambon, 5 years ago

Cc: pascal chambon added

Error confirmed on Django2.2.1, it makes the debug page much less helpful in case of TemplateError, since most websites inherit their templates from some kind of "base.html" skeleton.

comment:10 by Simone Lazzaris, 5 years ago

Bug still present in 2.2.3; if you (like me) use a tree-structured number of small template files it is very VERY annoying as you have to guess where the bug you're chasing is between the various parts.

comment:11 by Simone Lazzaris, 5 years ago

Cc: Simone Lazzaris added
Version: 2.02.2

comment:12 by Mariusz Felisiak, 5 years ago

Owner: Ankur Jain removed
Status: assignednew
Version: 2.2master

Simone, patch is welcome.

comment:13 by Min ho Kim, 5 years ago

Cc: Min ho Kim added

comment:14 by Min ho Kim, 5 years ago

Owner: set to Min ho Kim
Status: newassigned

comment:15 by Min ho Kim, 4 years ago

Owner: Min ho Kim removed
Status: assignednew

I'm deassigning myself for now so someone else can have a go until I can come back in the future.

comment:16 by Ashutosh Sharma, 4 years ago

Owner: set to Ashutosh Sharma
Status: newassigned

comment:17 by Ashutosh Sharma, 4 years ago

Owner: Ashutosh Sharma removed
Status: assignednew

I couldn't find a way to fix this bug. Deassigning so someone else can have a try.

comment:18 by Zahid Hussain Ansari, 4 years ago

Owner: set to Zahid Hussain Ansari
Status: newassigned

comment:19 by Mariusz Felisiak, 4 years ago

#31478 was a duplicate.

comment:20 by Tim Graham, 3 years ago

Owner: Zahid Hussain Ansari removed
Status: assignednew

comment:21 by cammil, 3 years ago

Owner: set to cammil
Status: newassigned

comment:22 by cammil, 3 years ago

Think I have an idea how to fix this. Going to do some more testing to make sure.

comment:23 by cammil, 3 years ago

I've created a PR: https://github.com/django/django/pull/14367

Does anyone have any comments or feedback?

Last edited 3 years ago by cammil (previous) (diff)

comment:24 by cammil, 3 years ago

Cc: cammil added
Has patch: set

comment:25 by Mariusz Felisiak, 3 years ago

Triage Stage: AcceptedReady for checkin

comment:26 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

Resolution: fixed
Status: assignedclosed

In 313c3d1:

Fixed #28935 -- Fixed display of errors in extended blocks.

Get the template that caused the exception and get the
exception info from that template, using the node that
caused the exception.

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