Author: kmtracey
Date: 2010-03-01 17:05:35 -0600 (Mon, 01 Mar 2010)
New Revision: 12643

Modified:
   django/trunk/django/template/__init__.py
   django/trunk/django/template/loader.py
   django/trunk/django/template/loaders/cached.py
   django/trunk/tests/regressiontests/templates/tests.py
Log:
Fixed #12992: Adjusted the new template loader code so that the template
file name is correctly reported on the debug page when a template syntax
error is raised.


Modified: django/trunk/django/template/__init__.py
===================================================================
--- django/trunk/django/template/__init__.py    2010-03-01 20:32:55 UTC (rev 
12642)
+++ django/trunk/django/template/__init__.py    2010-03-01 23:05:35 UTC (rev 
12643)
@@ -87,7 +87,7 @@
 
 # what to report as the origin for templates that come from non-loader sources
 # (e.g. strings)
-UNKNOWN_SOURCE="<unknown source>"
+UNKNOWN_SOURCE = '<unknown source>'
 
 # match a variable or block tag and capture the entire tag, including 
start/end delimiters
 tag_re = re.compile('(%s.*?%s|%s.*?%s|%s.*?%s)' % (re.escape(BLOCK_TAG_START), 
re.escape(BLOCK_TAG_END),

Modified: django/trunk/django/template/loader.py
===================================================================
--- django/trunk/django/template/loader.py      2010-03-01 20:32:55 UTC (rev 
12642)
+++ django/trunk/django/template/loader.py      2010-03-01 23:05:35 UTC (rev 
12643)
@@ -12,6 +12,11 @@
 # might be shown to the user for debugging purposes, so it should identify 
where
 # the template was loaded from.
 #
+# A loader may return an already-compiled template instead of the actual
+# template source. In that case the path returned should be None, since the
+# path information is associated with the template during the compilation,
+# which has already been done.
+#
 # Each loader should have an "is_usable" attribute set. This is a boolean that
 # specifies whether the loader can be used in this Python installation. Each
 # loader is responsible for setting this when it's initialized.
@@ -37,9 +42,10 @@
         return self.load_template(template_name, template_dirs)
 
     def load_template(self, template_name, template_dirs=None):
-        source, origin = self.load_template_source(template_name, 
template_dirs)
-        template = get_template_from_string(source, name=template_name)
-        return template, origin
+        source, display_name = self.load_template_source(template_name, 
template_dirs)
+        origin = make_origin(display_name, self.load_template_source, 
template_name, template_dirs)
+        template = get_template_from_string(source, origin, template_name)
+        return template, None
 
     def load_template_source(self, template_name, template_dirs=None):
         """
@@ -66,7 +72,7 @@
         return self.loader(self.loadname, self.dirs)[0]
 
 def make_origin(display_name, loader, name, dirs):
-    if settings.TEMPLATE_DEBUG:
+    if settings.TEMPLATE_DEBUG and display_name:
         return LoaderOrigin(display_name, loader, name, dirs)
     else:
         return None

Modified: django/trunk/django/template/loaders/cached.py
===================================================================
--- django/trunk/django/template/loaders/cached.py      2010-03-01 20:32:55 UTC 
(rev 12642)
+++ django/trunk/django/template/loaders/cached.py      2010-03-01 23:05:35 UTC 
(rev 12643)
@@ -38,8 +38,8 @@
             template, origin = self.find_template(template_name, template_dirs)
             if not hasattr(template, 'render'):
                 template = get_template_from_string(template, origin, 
template_name)
-            self.template_cache[template_name] = (template, origin)
-        return self.template_cache[template_name]
+            self.template_cache[template_name] = template
+        return self.template_cache[template_name], None
 
     def reset(self):
         "Empty the template cache."

Modified: django/trunk/tests/regressiontests/templates/tests.py
===================================================================
--- django/trunk/tests/regressiontests/templates/tests.py       2010-03-01 
20:32:55 UTC (rev 12642)
+++ django/trunk/tests/regressiontests/templates/tests.py       2010-03-01 
23:05:35 UTC (rev 12643)
@@ -155,6 +155,43 @@
             test_template_sources('/DIR1/index.HTML', template_dirs,
                                   ['/dir1/index.html'])
 
+    def test_loader_debug_origin(self):
+        # Turn TEMPLATE_DEBUG on, so that the origin file name will be kept 
with
+        # the compiled templates.
+        old_td, settings.TEMPLATE_DEBUG = settings.TEMPLATE_DEBUG, True
+
+        old_loaders = loader.template_source_loaders
+        loader.template_source_loaders = (filesystem.Loader(),)
+
+        # We rely on the fact that runtests.py sets up TEMPLATE_DIRS to
+        # point to a directory containing a 404.html file. Also that
+        # the file system and app directories loaders both inherit the
+        # load_template method from the BaseLoader class, so we only need
+        # to test one of them.
+        load_name = '404.html'
+        template = loader.get_template(load_name)
+        template_name = template.nodelist[0].source[0].name
+        self.assertTrue(template_name.endswith(load_name),
+            'Template loaded by filesystem loader has incorrect name for debug 
page: %s' % template_name)
+
+        # Aso test the cached loader, since it overrides load_template
+        cache_loader = cached.Loader(('',))
+        cache_loader._cached_loaders = loader.template_source_loaders
+        loader.template_source_loaders = (cache_loader,)
+
+        template = loader.get_template(load_name)
+        template_name = template.nodelist[0].source[0].name
+        self.assertTrue(template_name.endswith(load_name),
+            'Template loaded through cached loader has incorrect name for 
debug page: %s' % template_name)
+
+        template = loader.get_template(load_name)
+        template_name = template.nodelist[0].source[0].name
+        self.assertTrue(template_name.endswith(load_name),
+            'Cached template loaded through cached loader has incorrect name 
for debug page: %s' % template_name)
+
+        loader.template_source_loaders = old_loaders
+        settings.TEMPLATE_DEBUG = old_td
+
     def test_token_smart_split(self):
         # Regression test for #7027
         token = template.Token(template.TOKEN_BLOCK, 'sometag _("Page not 
found") value|yesno:_("yes,no")')

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-upda...@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to