Opened 14 years ago

Closed 11 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: dev
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


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 Peter Bengtsson 14 years ago.

Download all attachments as: .zip

Change History (9)

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

Same applies for TypeError.

comment:2 Changed 14 years ago by Alex Gaynor

Triage Stage: UnreviewedDesign decision needed

comment:3 Changed 14 years ago by anonymous

Cc: mpjung@… added

comment:4 Changed 14 years ago by Peter Bengtsson

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 14 years ago by Peter Bengtsson

Version: 1.0SVN

Changed 14 years ago by Peter Bengtsson

Attachment: 11421.patch added

comment:6 Changed 12 years ago by Julien Phalip

Has patch: set
Needs documentation: set
Severity: Normal
Type: New feature

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

comment:7 Changed 12 years ago by Thejaswi Puthraya

Component: UncategorizedTemplate system
Easy pickings: unset
UI/UX: unset

comment:8 Changed 11 years ago by Aymeric Augustin

Resolution: fixed
Status: newclosed

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:

{{ }}

With DEBUG = True you get the debug page.

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