Code

Opened 5 years ago

Closed 2 years ago

#11421 closed New feature (fixed)

Django should not silently ignore AttributeErrors raised in functions called by template

Reported by: Thomas Steinacher <tom@…> Owned by: nobody
Component: Template system Version: master
Severity: Normal Keywords:
Cc: mpjung@… Triage Stage: Design decision needed
Has patch: yes Needs documentation: yes
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Consider atemplate

{{ obj.some_function }}

where obj's some_function exists but raises an AttributeError because of a bug in the function's code. Django silently ignores the error and displays an empty string instead of a traceback, which could save much time when debugging.

Probably this beahaviour is because Django returns silently if we call an attribute that does not exist. But ideally it should only fail silently if the function does not exist, not if it raises an AttributeError. Therefore Django should first check using getattr() if the function exists and THEN call it, and not catch AttributeError.

Attachments (1)

11421.patch (3.4 KB) - added by peterbe 4 years ago.

Download all attachments as: .zip

Change History (9)

comment:1 Changed 5 years ago by Thomas Steinacher <tom@…>

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Same applies for TypeError.

comment:2 Changed 5 years ago by Alex

  • Triage Stage changed from Unreviewed to Design decision needed

comment:3 Changed 4 years ago by anonymous

  • Cc mpjung@… added

comment:4 Changed 4 years ago by peterbe

I created this patch based on 1.2 pre-alpha SVN-11617

Before this patch, inside _resolve_lookup(), it lump together AttributeErrors you get when doing getattr(current, bit) with AttributeErrors you get within current=current() when we in fact should treat them as two different errors. Django templates fail silently, by definition, on lookups that as in "it doesn't exist" but allows errors within propagate once found.

The patch still doesn't solve the problem with TypeErrors. Next patch maybe or a separate ticket.

comment:5 Changed 4 years ago by peterbe

  • Version changed from 1.0 to SVN

Changed 4 years ago by peterbe

comment:6 Changed 3 years ago by julien

  • Has patch set
  • Needs documentation set
  • Severity set to Normal
  • Type set to New feature

See #6907 for a related issue. I was almost tempted to close this one as a dupe.

comment:7 Changed 3 years ago by thejaswi_puthraya

  • Component changed from Uncategorized to Template system
  • Easy pickings unset
  • UI/UX unset

comment:8 Changed 2 years ago by aaugustin

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

This is fixed in trunk.

Test 1

In a view, define:

    def foo():
        raise AttributeError

and put foo in the context.

In a template, render:

{{ foo }}

With DEBUG = True you get the debug page.

Test 2

In a view, define:

class MyObj(object):
    def foo(self):
        raise AttributeError
obj = MyObj()

and put obj in the context.

In a template, render:

{{ obj.foo }}

With DEBUG = True you get the debug page.

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.