Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed 19949 -- Cached template loader now caches TemplateDoesNotExist

Thanks @timgraham and @jdunck for the code reviews and Kronuz for bug
report and initial patch.
  • Loading branch information...
commit f33db5a09acfc3df3085235a5712c46094eb9a0d 1 parent 35230ad
Susan  Tan authored timgraham committed
14  django/template/loaders/cached.py
@@ -57,9 +57,11 @@ def find_template(self, name, dirs=None):
57 57
 
58 58
     def load_template(self, template_name, template_dirs=None):
59 59
         key = self.cache_key(template_name, template_dirs)
60  
-        try:
61  
-            template = self.template_cache[key]
62  
-        except KeyError:
  60
+        template_tuple = self.template_cache.get(key)
  61
+        # cached a previous failure:
  62
+        if template_tuple is TemplateDoesNotExist:
  63
+            raise TemplateDoesNotExist
  64
+        elif template_tuple is None:
63 65
             template, origin = self.find_template(template_name, template_dirs)
64 66
             if not hasattr(template, 'render'):
65 67
                 try:
@@ -69,9 +71,9 @@ def load_template(self, template_name, template_dirs=None):
69 71
                     # back off to returning the source and display name for the template
70 72
                     # we were asked to load. This allows for correct identification (later)
71 73
                     # of the actual template that does not exist.
72  
-                    return template, origin
73  
-            self.template_cache[key] = template
74  
-        return template, None
  74
+                    self.template_cache[key] = (template, origin)
  75
+            self.template_cache[key] = (template, None)
  76
+        return self.template_cache[key]
75 77
 
76 78
     def reset(self):
77 79
         "Empty the template cache."
11  tests/template_tests/test_loaders.py
@@ -105,6 +105,7 @@ def test_not_installed(self):
105 105
         egg_loader = EggLoader()
106 106
         self.assertRaises(TemplateDoesNotExist, egg_loader.load_template_source, "y.html")
107 107
 
  108
+
108 109
 class CachedLoader(unittest.TestCase):
109 110
     def setUp(self):
110 111
         self.old_TEMPLATE_LOADERS = settings.TEMPLATE_LOADERS
@@ -127,6 +128,16 @@ def test_templatedir_caching(self):
127 128
         # The two templates should not have the same content
128 129
         self.assertNotEqual(t1.render(Context({})), t2.render(Context({})))
129 130
 
  131
+    def test_missing_template_is_cached(self):
  132
+        "Check that the missing template is cached."
  133
+        template_loader = loader.find_template_loader(settings.TEMPLATE_LOADERS[0])
  134
+        # Empty cache, which may be filled from previous tests.
  135
+        template_loader.reset()
  136
+        # Check that 'missing.html' isn't already in cache before 'missing.html' is loaed
  137
+        self.assertRaises(KeyError, lambda: template_loader.template_cache["missing.html"])
  138
+        self.assertRaises(TemplateDoesNotExist, template_loader.load_template, "missing.html")
  139
+
  140
+
130 141
 class RenderToStringTest(unittest.TestCase):
131 142
 
132 143
     def setUp(self):

0 notes on commit f33db5a

Please sign in to comment.
Something went wrong with that request. Please try again.