Opened 2 years ago

Closed 16 months 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: anubhav9042
Component: Template system Version: master
Severity: Normal Keywords:
Cc: timo Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


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 anubhav9042 17 months ago.
21707_patch2.diff (1.6 KB) - added by anubhav9042 16 months ago.

Download all attachments as: .zip

Change History (11)

comment:1 Changed 2 years ago by timo

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted
  • Version changed from 1.4 to master

comment:2 Changed 18 months ago by anubhav9042

  • Owner changed from nobody to anubhav9042
  • Status changed from new to assigned

Changed 17 months ago by anubhav9042

comment:3 Changed 17 months ago by anubhav9042

  • Has patch set

I have added a small patch which raises TemplateSyntaxError.

comment:4 Changed 17 months ago by timo

  • Cc timo 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 Changed 17 months ago by anubhav9042

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

Changed 16 months ago by anubhav9042

comment:6 Changed 16 months ago by anubhav9042

Changed the position where I handled AttributeError

comment:7 Changed 16 months ago by timo

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 Changed 16 months ago by anubhav9042

  • Patch needs improvement unset

comment:9 Changed 16 months ago by Tim Graham <timograham@…>

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

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