Ticket #19949: #19949-cache_non_existent_templates.diff

File #19949-cache_non_existent_templates.diff, 2.3 KB (added by German M. Bravo, 12 years ago)
  • django/template/loaders/cached.py

    class Loader(BaseLoader):  
    4242            # If template directories were specified, use a hash to differentiate
    4343            key = '-'.join([template_name, hashlib.sha1('|'.join(template_dirs)).hexdigest()])
    4444
    45         if key not in self.template_cache:
    46             template, origin = self.find_template(template_name, template_dirs)
    47             if not hasattr(template, 'render'):
    48                 try:
    49                     template = get_template_from_string(template, origin, template_name)
    50                 except TemplateDoesNotExist:
    51                     # If compiling the template we found raises TemplateDoesNotExist,
    52                     # back off to returning the source and display name for the template
    53                     # we were asked to load. This allows for correct identification (later)
    54                     # of the actual template that does not exist.
    55                     return template, origin
    56             self.template_cache[key] = template
    57         return self.template_cache[key], None
     45        template_tuple = self.template_cache.get(key)
     46        if template_tuple is None:
     47            try:
     48                template, origin = self.find_template(template_name, template_dirs)
     49            except TemplateDoesNotExist:
     50                template_tuple = TemplateDoesNotExist
     51            else:
     52                if not hasattr(template, 'render'):
     53                    try:
     54                        template = get_template_from_string(template, origin, template_name)
     55                    except TemplateDoesNotExist:
     56                        # If compiling the template we found raises TemplateDoesNotExist,
     57                        # back off to returning the source and display name for the template
     58                        # we were asked to load. This allows for correct identification (later)
     59                        # of the actual template that does not exist.
     60                        pass
     61                template_tuple = (template, origin)
     62            self.template_cache[key] = template_tuple
     63        if template_tuple is TemplateDoesNotExist:
     64            raise TemplateDoesNotExist
     65        return template_tuple
    5866
    5967    def reset(self):
    6068        "Empty the template cache."
Back to Top