Ticket #9154: templates_optimizations_r9076.2.diff

File templates_optimizations_r9076.2.diff, 4.6 KB (added by msaelices, 7 years ago)

New patch that fixes a rare inheritance case.

  • django/contrib/admin/templatetags/log.py

     
    1515            context[self.varname] = LogEntry.objects.all().select_related('content_type', 'user')[:self.limit]
    1616        else:
    1717            if not self.user.isdigit():
    18                 self.user = context[self.user].id
    19             context[self.varname] = LogEntry.objects.filter(user__id__exact=self.user).select_related('content_type', 'user')[:self.limit]
     18                user_id = context[self.user].id
     19            context[self.varname] = LogEntry.objects.filter(user__id__exact=user_id).select_related('content_type', 'user')[:self.limit]
    2020        return ''
    2121
    2222class DoGetAdminLog:
  • django/template/loader_tags.py

     
     1import copy
     2
    13from django.template import TemplateSyntaxError, TemplateDoesNotExist, Variable
    24from django.template import Library, Node, TextNode
    3 from django.template.loader import get_template, get_template_from_string, find_template_source
     5from django.template.loader import get_template
    46from django.conf import settings
    57from django.utils.safestring import mark_safe
    68
     
    4345        self.nodelist = nodelist
    4446        self.parent_name, self.parent_name_expr = parent_name, parent_name_expr
    4547        self.template_dirs = template_dirs
     48        self.compiled_parent = copy.deepcopy(self.get_parent())
    4649
    4750    def __repr__(self):
    4851        if self.parent_name_expr:
    4952            return "<ExtendsNode: extends %s>" % self.parent_name_expr.token
    5053        return '<ExtendsNode: extends "%s">' % self.parent_name
    5154
    52     def get_parent(self, context):
    53         if self.parent_name_expr:
    54             self.parent_name = self.parent_name_expr.resolve(context)
     55    def get_parent(self):
    5556        parent = self.parent_name
    5657        if not parent:
    5758            error_msg = "Invalid template name in 'extends' tag: %r." % parent
     
    6162        if hasattr(parent, 'render'):
    6263            return parent # parent is a Template object
    6364        try:
    64             source, origin = find_template_source(parent, self.template_dirs)
     65            return get_template(parent, self.template_dirs)
    6566        except TemplateDoesNotExist:
    6667            raise TemplateSyntaxError, "Template %r cannot be extended, because it doesn't exist" % parent
    67         else:
    68             return get_template_from_string(source, origin, parent)
    6968
    7069    def render(self, context):
    71         compiled_parent = self.get_parent(context)
     70        compiled_parent = self.compiled_parent
    7271        parent_blocks = dict([(n.name, n) for n in compiled_parent.nodelist.get_nodes_by_type(BlockNode)])
    7372        for block_node in self.nodelist.get_nodes_by_type(BlockNode):
    7473            # Check for a BlockNode with this node's name, and replace it if found.
     
    156155    uses the literal value "base" as the name of the parent template to extend,
    157156    or ``{% extends variable %}`` uses the value of ``variable`` as either the
    158157    name of the parent template to extend (if it evaluates to a string) or as
    159     the parent tempate itelf (if it evaluates to a Template object).
     158    the parent template itself (if it evaluates to a Template object).
    160159    """
    161160    bits = token.contents.split()
    162161    if len(bits) != 2:
  • django/template/loader.py

     
    2626
    2727template_source_loaders = None
    2828
     29_template_cache = {}
     30
    2931class LoaderOrigin(Origin):
    3032    def __init__(self, display_name, loader, name, dirs):
    3133        super(LoaderOrigin, self).__init__(display_name)
     
    7274            pass
    7375    raise TemplateDoesNotExist, name
    7476
    75 def get_template(template_name):
     77def get_template(template_name, dirs=None, from_child=None):
    7678    """
    7779    Returns a compiled Template object for the given template name,
    7880    handling template inheritance recursively.
    7981    """
    80     source, origin = find_template_source(template_name)
     82    if template_name in _template_cache:
     83        return _template_cache[template_name]
     84    source, origin = find_template_source(template_name, dirs)
    8185    template = get_template_from_string(source, origin, template_name)
     86    _template_cache[template_name] = template
    8287    return template
    8388
    8489def get_template_from_string(source, origin=None, name=None):
Back to Top