Skip to content

Commit 8a6cb3d

Browse files
committed
Fixed django#13573 -- Corrected problem with template caching when template directories are provided. Thanks to lamby for the report.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13295 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 6b2fd34 commit 8a6cb3d

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed

django/template/loaders/cached.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,22 @@ def find_template(self, name, dirs=None):
3434
raise TemplateDoesNotExist(name)
3535

3636
def load_template(self, template_name, template_dirs=None):
37-
if template_name not in self.template_cache:
37+
# Use hash(..) to avoid saving potentially large template_dirs values
38+
key = hash((template_name, template_dirs))
39+
40+
if key not in self.template_cache:
3841
template, origin = self.find_template(template_name, template_dirs)
3942
if not hasattr(template, 'render'):
4043
try:
4144
template = get_template_from_string(template, origin, template_name)
4245
except TemplateDoesNotExist:
43-
# If compiling the template we found raises TemplateDoesNotExist,
44-
# back off to returning the source and display name for the template
45-
# we were asked to load. This allows for correct identification (later)
46+
# If compiling the template we found raises TemplateDoesNotExist,
47+
# back off to returning the source and display name for the template
48+
# we were asked to load. This allows for correct identification (later)
4649
# of the actual template that does not exist.
4750
return template, origin
48-
self.template_cache[template_name] = template
49-
return self.template_cache[template_name], None
51+
self.template_cache[key] = template
52+
return self.template_cache[key], None
5053

5154
def reset(self):
5255
"Empty the template cache."

tests/regressiontests/templates/loaders.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
import StringIO
1717
import os.path
1818

19-
from django.template import TemplateDoesNotExist
19+
from django.template import TemplateDoesNotExist, Context
2020
from django.template.loaders.eggs import load_template_source as lts_egg
21+
from django.template import loader
2122

2223
# Mock classes and objects for pkg_resources functions.
2324
class MockProvider(pkg_resources.NullProvider):
@@ -89,5 +90,27 @@ def test_not_installed(self):
8990
settings.INSTALLED_APPS = []
9091
self.assertRaises(TemplateDoesNotExist, lts_egg, "y.html")
9192

93+
class CachedLoader(unittest.TestCase):
94+
def setUp(self):
95+
self.old_TEMPLATE_LOADERS = settings.TEMPLATE_LOADERS
96+
settings.TEMPLATE_LOADERS = (
97+
('django.template.loaders.cached.Loader', (
98+
'django.template.loaders.filesystem.Loader',
99+
)
100+
),
101+
)
102+
def tearDown(self):
103+
settings.TEMPLATE_LOADERS = self.old_TEMPLATE_LOADERS
104+
105+
def test_templatedir_caching(self):
106+
"Check that the template directories form part of the template cache key. Refs #13573"
107+
# Retrive a template specifying a template directory to check
108+
t1, name = loader.find_template('test.html', (os.path.join(os.path.dirname(__file__), 'templates', 'first'),))
109+
# Now retrieve the same template name, but from a different directory
110+
t2, name = loader.find_template('test.html', (os.path.join(os.path.dirname(__file__), 'templates', 'second'),))
111+
112+
# The two templates should not have the same content
113+
self.assertNotEqual(t1.render(Context({})), t2.render(Context({})))
114+
92115
if __name__ == "__main__":
93116
unittest.main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
First template
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Second template

0 commit comments

Comments
 (0)