Fixed #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
This commit is contained in:
parent
6b2fd346ad
commit
8a6cb3d969
|
@ -34,19 +34,22 @@ class Loader(BaseLoader):
|
||||||
raise TemplateDoesNotExist(name)
|
raise TemplateDoesNotExist(name)
|
||||||
|
|
||||||
def load_template(self, template_name, template_dirs=None):
|
def load_template(self, template_name, template_dirs=None):
|
||||||
if template_name not in self.template_cache:
|
# Use hash(..) to avoid saving potentially large template_dirs values
|
||||||
|
key = hash((template_name, template_dirs))
|
||||||
|
|
||||||
|
if key not in self.template_cache:
|
||||||
template, origin = self.find_template(template_name, template_dirs)
|
template, origin = self.find_template(template_name, template_dirs)
|
||||||
if not hasattr(template, 'render'):
|
if not hasattr(template, 'render'):
|
||||||
try:
|
try:
|
||||||
template = get_template_from_string(template, origin, template_name)
|
template = get_template_from_string(template, origin, template_name)
|
||||||
except TemplateDoesNotExist:
|
except TemplateDoesNotExist:
|
||||||
# If compiling the template we found raises TemplateDoesNotExist,
|
# If compiling the template we found raises TemplateDoesNotExist,
|
||||||
# back off to returning the source and display name for the template
|
# back off to returning the source and display name for the template
|
||||||
# we were asked to load. This allows for correct identification (later)
|
# we were asked to load. This allows for correct identification (later)
|
||||||
# of the actual template that does not exist.
|
# of the actual template that does not exist.
|
||||||
return template, origin
|
return template, origin
|
||||||
self.template_cache[template_name] = template
|
self.template_cache[key] = template
|
||||||
return self.template_cache[template_name], None
|
return self.template_cache[key], None
|
||||||
|
|
||||||
def reset(self):
|
def reset(self):
|
||||||
"Empty the template cache."
|
"Empty the template cache."
|
||||||
|
|
|
@ -16,8 +16,9 @@ import imp
|
||||||
import StringIO
|
import StringIO
|
||||||
import os.path
|
import os.path
|
||||||
|
|
||||||
from django.template import TemplateDoesNotExist
|
from django.template import TemplateDoesNotExist, Context
|
||||||
from django.template.loaders.eggs import load_template_source as lts_egg
|
from django.template.loaders.eggs import load_template_source as lts_egg
|
||||||
|
from django.template import loader
|
||||||
|
|
||||||
# Mock classes and objects for pkg_resources functions.
|
# Mock classes and objects for pkg_resources functions.
|
||||||
class MockProvider(pkg_resources.NullProvider):
|
class MockProvider(pkg_resources.NullProvider):
|
||||||
|
@ -89,5 +90,27 @@ class EggLoader(unittest.TestCase):
|
||||||
settings.INSTALLED_APPS = []
|
settings.INSTALLED_APPS = []
|
||||||
self.assertRaises(TemplateDoesNotExist, lts_egg, "y.html")
|
self.assertRaises(TemplateDoesNotExist, lts_egg, "y.html")
|
||||||
|
|
||||||
|
class CachedLoader(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.old_TEMPLATE_LOADERS = settings.TEMPLATE_LOADERS
|
||||||
|
settings.TEMPLATE_LOADERS = (
|
||||||
|
('django.template.loaders.cached.Loader', (
|
||||||
|
'django.template.loaders.filesystem.Loader',
|
||||||
|
)
|
||||||
|
),
|
||||||
|
)
|
||||||
|
def tearDown(self):
|
||||||
|
settings.TEMPLATE_LOADERS = self.old_TEMPLATE_LOADERS
|
||||||
|
|
||||||
|
def test_templatedir_caching(self):
|
||||||
|
"Check that the template directories form part of the template cache key. Refs #13573"
|
||||||
|
# Retrive a template specifying a template directory to check
|
||||||
|
t1, name = loader.find_template('test.html', (os.path.join(os.path.dirname(__file__), 'templates', 'first'),))
|
||||||
|
# Now retrieve the same template name, but from a different directory
|
||||||
|
t2, name = loader.find_template('test.html', (os.path.join(os.path.dirname(__file__), 'templates', 'second'),))
|
||||||
|
|
||||||
|
# The two templates should not have the same content
|
||||||
|
self.assertNotEqual(t1.render(Context({})), t2.render(Context({})))
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
First template
|
|
@ -0,0 +1 @@
|
||||||
|
Second template
|
Loading…
Reference in New Issue