Code

#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

Description

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@… 14 months ago.
Example

Download all attachments as: .zip

Change History (5)

Changed 14 months ago by michaelvantellingen@…

Example

comment:1 Changed 14 months ago by michaelvantellingen@…

  • Has patch set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 14 months ago by aaugustin

  • Patch needs improvement set
  • Triage Stage changed from Unreviewed to Accepted

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.

Thanks!

comment:3 Changed 14 months 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:

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

comment:4 Changed 14 months ago by Aymeric Augustin <aymeric.augustin@…>

  • Resolution set to fixed
  • Status changed from new to closed

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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.