Opened 15 years ago

Closed 13 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

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

Download all attachments as: .zip

Change History (9)

comment:1 by Thomas Steinacher <tom@…>, 15 years ago

Same applies for TypeError.

comment:2 by Alex Gaynor, 15 years ago

Triage Stage: UnreviewedDesign decision needed

comment:3 by anonymous, 15 years ago

Cc: mpjung@… added

comment:4 by Peter Bengtsson, 15 years ago

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

Version: 1.0SVN

by Peter Bengtsson, 15 years ago

Attachment: 11421.patch added

comment:6 by Julien Phalip, 14 years ago

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 by Thejaswi Puthraya, 14 years ago

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

comment:8 by Aymeric Augustin, 13 years ago

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:

{{ obj.foo }}

With DEBUG = True you get the debug page.

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