Opened 11 years ago

Closed 10 years ago

#21707 closed Cleanup/optimization (fixed)

Uninformative error if block.super is used inside the block tag of a base template

Reported by: Mitar Owned by: ANUBHAV JOSHI
Component: Template system Version: dev
Severity: Normal Keywords:
Cc: Tim Graham Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

If block.super is used inside the block tag of a base template, an uninformative error is thrown:

'BlockNode' object has no attribute 'context'

It is thrown in super method when doing:

render_context = self.context.render_context

I think code should check for missing self.context and throw a more informative exception.

Attachments (2)

21707.patch (1.5 KB ) - added by ANUBHAV JOSHI 10 years ago.
21707_patch2.diff (1.6 KB ) - added by ANUBHAV JOSHI 10 years ago.

Download all attachments as: .zip

Change History (11)

comment:1 by Tim Graham, 11 years ago

Triage Stage: UnreviewedAccepted
Version: 1.4master

comment:2 by ANUBHAV JOSHI, 10 years ago

Owner: changed from nobody to ANUBHAV JOSHI
Status: newassigned

by ANUBHAV JOSHI, 10 years ago

Attachment: 21707.patch added

comment:3 by ANUBHAV JOSHI, 10 years ago

Has patch: set

I have added a small patch which raises TemplateSyntaxError.

comment:4 by Tim Graham, 10 years ago

Cc: Tim Graham added
Patch needs improvement: set

I would hope there's a more specific way to detect this problem than catching all AttributeErrors. Also TemplateSyntaxError should include a helpful message.

comment:5 by ANUBHAV JOSHI, 10 years ago

Sure.
I just had a quick look. I'll add a better patch soon.

by ANUBHAV JOSHI, 10 years ago

Attachment: 21707_patch2.diff added

comment:6 by ANUBHAV JOSHI, 10 years ago

Changed the position where I handled AttributeError

comment:7 by Tim Graham, 10 years ago

Rather than try/except, it would probably be better to do:

if not hasattr(self, 'context'):
    raise TemplateSyntaxError(...)

And make the message less definitive (e.g. include the original exception message and add "Did you use {{ block.super }} in a base template?". I am not sure, but there might be other cases where context could be missing so including the original exception could be helpful just in case.

comment:8 by ANUBHAV JOSHI, 10 years ago

Patch needs improvement: unset

comment:9 by Tim Graham <timograham@…>, 10 years ago

Resolution: fixed
Status: assignedclosed

In b1abfb3c59467d0bb088e19be76fc42fcb570835:

Fixed #21707 -- Added helpful error message when using {{ block.super }} in base template.

Thanks mitar for the suggestion.

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