Opened 6 years ago

Closed 6 years ago

#19819 closed Bug (fixed)

Django highlights incorrect lines for template errors

Reported by: michaelvantellingen@… Owned by: nobody
Component: Template system Version: 1.4
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no


This issue is bugging me for a while now and after searching for existing tickets (and finding none) i've decided to create a ticket for it.

Basically Django highlights the wrong lines when a template error occurs. Sometimes it even highlights a seemingly random line in the parent template (when an error occurs in the template which is included).

I'll attach a screenshot with an example.

Attachments (1)

Screen Shot 2013-02-13 at 15.36.56.png (47.7 KB) - added by michaelvantellingen@… 6 years ago.

Download all attachments as: .zip

Change History (5)

Changed 6 years ago by michaelvantellingen@…


comment:1 Changed 6 years ago by michaelvantellingen@…

Has patch: set

comment:2 Changed 6 years ago by Aymeric Augustin

Patch needs improvement: set
Triage Stage: UnreviewedAccepted

The patch looks quite good, but it needs a bit of polish.

First, I suggest to redo the PR against master.

You must support Python 2.6+ and 3.2+. Specifically, use the syntax except TemplateSyntaxError as e: to catch exceptions.

To test an exception, there's assertRaisesRegexp. Since it was renamed in Python 3, you must use this slightly contrived syntax:

from django.utils import six

with six.assertRaisesRegex(self, TemplateSyntaxError, ...):
    # raise exception here

Finally, make sure the code is clean — there a suprious print statement and a stray newline in a docstring.


comment:3 Changed 6 years ago by michaelvantellingen@…

Thanks for the feedback! Here is a new PR against master. Tested it with python 2.6, 2.7 and 3.2:

comment:4 Changed 6 years ago by Aymeric Augustin <aymeric.augustin@…>

Resolution: fixed
Status: newclosed

In 138de533ff677b470a1e7b4b6ff084a5b7a7444b:

Fixed #19819 - Improved template filter errors handling.

Wrap the Parser.compile_filter method call with a try/except and call the
newly added Parser.compile_filter_error(). Overwrite this method in the
DebugParser to throw the correct error.

Since this error was otherwise catched by the compile_function try/except
block the debugger highlighted the wrong line.

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