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: | 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)
Change History (9)
comment:1 by , 15 years ago
comment:2 by , 15 years ago
Triage Stage: | Unreviewed → Design decision needed |
---|
comment:3 by , 15 years ago
Cc: | added |
---|
comment:4 by , 15 years ago
I created this patch based on 1.2 pre-alpha SVN-11617
Before this patch, inside _resolve_lookup()
, it lump together AttributeError
s you get when doing getattr(current, bit)
with AttributeError
s 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 TypeError
s. Next patch maybe or a separate ticket.
comment:5 by , 15 years ago
Version: | 1.0 → SVN |
---|
by , 15 years ago
Attachment: | 11421.patch added |
---|
comment:6 by , 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 , 14 years ago
Component: | Uncategorized → Template system |
---|---|
Easy pickings: | unset |
UI/UX: | unset |
comment:8 by , 13 years ago
Resolution: | → fixed |
---|---|
Status: | new → 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.
Same applies for TypeError.