Opened 3 months ago

Closed 2 months ago

#35789 closed Cleanup/optimization (fixed)

Improve non-first {% extends %} error message

Reported by: Adam Johnson Owned by: Ekin Ertaç
Component: Template system Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

Currently if you put {% extends %} after another tag, the error looks like:

TemplateSyntaxError: <ExtendsNode: extends 'base.html'> must be the first tag in the template.

ExtendsNode is a leaked internal detail. Showing its repr() is a bit confusing, especially for a beginner programmer.

The message would be clearer if it showed the actual text of the tag:

TemplateSyntaxError: {% extends 'base.html' %} must be the first tag in the template.

Change History (9)

comment:1 by Claude Paroz, 3 months ago

Easy pickings: set
Triage Stage: UnreviewedAccepted

comment:2 by Mohamed Nabil Rady, 3 months ago

Owner: set to Mohamed Nabil Rady
Status: newassigned

comment:3 by Mohamed Nabil Rady, 3 months ago

So After looking at the code I am a bit stuck on what should be done, the error message is constructed using the __repr__ method of ExtendNode class

    # The way the error is constructed
    if node.must_be_first and nodelist.contains_nontext:
        raise self.error(
            token,
            "%r must be the first tag in the template." % node,
        )

And this is the __repr__ function

    # __repr__ function for ExtendsNode class
    def __repr__(self):
        return "<%s: extends %s>" % (self.__class__.__name__, self.parent_name.token)

I don't think changing the repr method or hard coding the error message is any better. So what should be done ?

comment:4 by Adam Johnson, 3 months ago

The repr should not be changed, it’s still used in other contexts like debugging. Change the error message to show/build the extends tag source.

comment:5 by Mohamed Nabil Rady, 3 months ago

Has patch: set

comment:6 by Ekin Ertaç, 3 months ago

Just sent a PR to github to add a clear error message. PR

Edit:
I didn't notice the first PR, sorry about that.

Last edited 3 months ago by Ekin Ertaç (previous) (diff)

comment:7 by Sarah Boyce, 2 months ago

Owner: changed from Mohamed Nabil Rady to Ekin Ertaç
Patch needs improvement: set

comment:8 by Sarah Boyce, 2 months ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:9 by Sarah Boyce <42296566+sarahboyce@…>, 2 months ago

Resolution: fixed
Status: assignedclosed

In 68cee15:

Fixed #35789 -- Improved the error message raised when the tag must be first in the template.

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