Opened 3 days ago

Last modified 3 hours ago

#35789 assigned Cleanup/optimization

Improve non-first {% extends %} error message

Reported by: Adam Johnson Owned by: Mohamed Nabil Rady
Component: Template system Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
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 (5)

comment:1 by Claude Paroz, 41 hours ago

Easy pickings: set
Triage Stage: Unreviewed β†’ Accepted

comment:2 by Mohamed Nabil Rady, 30 hours ago

Owner: set to Mohamed Nabil Rady
Status: new β†’ assigned

comment:3 by Mohamed Nabil Rady, 12 hours 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, 10 hours 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 hours ago

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