Ticket #17660: 17660a.patch

File 17660a.patch, 2.9 KB (added by FunkyBob, 4 years ago)

After further discussion with SmileyChris, here is a patch which doesn't lose the extra debugging info, instead showing the actual expression used.

  • django/template/loader_tags.py

     
    11from django.conf import settings
    2 from django.template.base import TemplateSyntaxError, Library, Node, TextNode, token_kwargs
     2from django.template.base import TemplateSyntaxError, Library, Node, TextNode, token_kwargs, Variable
    33from django.template.loader import get_template
    44from django.utils.safestring import mark_safe
    55
     
    7474class ExtendsNode(Node):
    7575    must_be_first = True
    7676
    77     def __init__(self, nodelist, parent_name, parent_name_expr, template_dirs=None):
     77    def __init__(self, nodelist, parent_name, template_dirs=None):
    7878        self.nodelist = nodelist
    79         self.parent_name, self.parent_name_expr = parent_name, parent_name_expr
     79        self.parent_name = parent_name
    8080        self.template_dirs = template_dirs
    8181        self.blocks = dict([(n.name, n) for n in nodelist.get_nodes_by_type(BlockNode)])
    8282
    8383    def __repr__(self):
    84         if self.parent_name_expr:
    85             return "<ExtendsNode: extends %s>" % self.parent_name_expr.token
    86         return '<ExtendsNode: extends "%s">' % self.parent_name
     84        return '<ExtendsNode: extends %s>' % self.parent_name.token
    8785
    8886    def get_parent(self, context):
    89         if self.parent_name_expr:
    90             parent = self.parent_name_expr.resolve(context)
    91         else:
    92             parent = self.parent_name
     87        parent = self.parent_name.resolve(context)
    9388        if not parent:
    9489            error_msg = "Invalid template name in 'extends' tag: %r." % parent
    95             if self.parent_name_expr:
    96                 error_msg += " Got this from the '%s' variable." % self.parent_name_expr.token
     90            if self.parent_name.filters or isinstance(self.parent_name.var, Variable):
     91                error_msg += " Got this from the '%s' variable." % self.parent_name.token
    9792            raise TemplateSyntaxError(error_msg)
    9893        if hasattr(parent, 'render'):
    9994            return parent # parent is a Template object
     
    212207    bits = token.split_contents()
    213208    if len(bits) != 2:
    214209        raise TemplateSyntaxError("'%s' takes one argument" % bits[0])
    215     parent_name, parent_name_expr = None, None
    216     if bits[1][0] in ('"', "'") and bits[1][-1] == bits[1][0]:
    217         parent_name = bits[1][1:-1]
    218     else:
    219         parent_name_expr = parser.compile_filter(bits[1])
     210    parent_name = parser.compile_filter(bits[1])
    220211    nodelist = parser.parse()
    221212    if nodelist.get_nodes_by_type(ExtendsNode):
    222213        raise TemplateSyntaxError("'%s' cannot appear more than once in the same template" % bits[0])
    223     return ExtendsNode(nodelist, parent_name, parent_name_expr)
     214    return ExtendsNode(nodelist, parent_name)
    224215
    225216@register.tag('include')
    226217def do_include(parser, token):
Back to Top