Index: django/template/loaders/cached.py
===================================================================
--- django/template/loaders/cached.py	(revision 12791)
+++ django/template/loaders/cached.py	(working copy)
@@ -37,7 +37,10 @@
         if template_name not in self.template_cache:
             template, origin = self.find_template(template_name, template_dirs)
             if not hasattr(template, 'render'):
-                template = get_template_from_string(template, origin, template_name)
+                try:
+                    template = get_template_from_string(template, origin, template_name)
+                except TemplateDoesNotExist:
+                    return template, origin
             self.template_cache[template_name] = template
         return self.template_cache[template_name], None
 
Index: django/template/loader_tags.py
===================================================================
--- django/template/loader_tags.py	(revision 12791)
+++ django/template/loader_tags.py	(working copy)
@@ -97,10 +97,7 @@
             raise TemplateSyntaxError(error_msg)
         if hasattr(parent, 'render'):
             return parent # parent is a Template object
-        try:
-            return get_template(parent)
-        except TemplateDoesNotExist:
-            raise TemplateSyntaxError("Template %r cannot be extended, because it doesn't exist" % parent)
+        return get_template(parent)
 
     def render(self, context):
         compiled_parent = self.get_parent(context)
Index: django/template/loader.py
===================================================================
--- django/template/loader.py	(revision 12791)
+++ django/template/loader.py	(working copy)
@@ -44,8 +44,11 @@
     def load_template(self, template_name, template_dirs=None):
         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
+        try:
+            template = get_template_from_string(source, origin, template_name)
+            return template, None
+        except TemplateDoesNotExist:
+            return source, display_name
 
     def load_template_source(self, template_name, template_dirs=None):
         """
Index: tests/regressiontests/templates/tests.py
===================================================================
--- tests/regressiontests/templates/tests.py	(revision 12791)
+++ tests/regressiontests/templates/tests.py	(working copy)
@@ -160,39 +160,90 @@
         # 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
 
+        try:
+            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)
+        finally:
+            loader.template_source_loaders = old_loaders
+            settings.TEMPLATE_DEBUG = old_td
+
+    def test_does_not_exist_name_app(self):
+        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)
+        try:
+            loader.template_source_loaders = (app_directories.Loader(),)
 
-        # 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,)
+            load_name = 'test_extends_error.html'
+            tmpl = loader.get_template(load_name)
+            r = None
+            try:
+                r = tmpl.render(template.Context({}))
+            except template.TemplateSyntaxError, e:
+                settings.TEMPLATE_DEBUG = old_td
+                self.assertEqual(e.args[0], 'Caught TemplateDoesNotExist while rendering: missing.html')
+            self.assertEqual(r, None, 'Template rendering unexpectedly succeeded, produced: ->%r<-' % r)
+        finally:
+            loader.template_source_loaders = old_loaders
+            settings.TEMPLATE_DEBUG = old_td
 
-        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)
+    def test_does_not_exist_name_cache(self):
+        old_td, settings.TEMPLATE_DEBUG = settings.TEMPLATE_DEBUG, True
+        old_loaders = loader.template_source_loaders
 
-        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)
+        try:
+            cache_loader = cached.Loader(('',))
+            cache_loader._cached_loaders = (app_directories.Loader(),)
+            loader.template_source_loaders = (cache_loader,)
 
-        loader.template_source_loaders = old_loaders
-        settings.TEMPLATE_DEBUG = old_td
+            load_name = 'test_extends_error.html'
+            tmpl = loader.get_template(load_name)
+            r = None
+            try:
+                r = tmpl.render(template.Context({}))
+            except template.TemplateSyntaxError, e:
+                self.assertEqual(e.args[0], 'Caught TemplateDoesNotExist while rendering: missing.html')
+            self.assertEqual(r, None, 'Template rendering unexpectedly succeeded, produced: ->%r<-' % r)
 
+            # For the cached loader, repeat the test, to ensure the first attempt did not cache a
+            # result that behaves incorrectly on subsequent attempts.
+            tmpl = loader.get_template(load_name)
+            try:
+                tmpl.render(template.Context({}))
+            except template.TemplateSyntaxError, e:
+                self.assertEqual(e.args[0], 'Caught TemplateDoesNotExist while rendering: missing.html')
+            self.assertEqual(r, None, 'Template rendering unexpectedly succeeded, produced: ->%r<-' % r)
+        finally:
+            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")')
@@ -269,7 +320,7 @@
             if isinstance(vals[2], tuple):
                 normal_string_result = vals[2][0]
                 invalid_string_result = vals[2][1]
-                if '%s' in invalid_string_result:
+                if isinstance(invalid_string_result, basestring) and '%s' in invalid_string_result:
                     expected_invalid_str = 'INVALID %s'
                     invalid_string_result = invalid_string_result % vals[2][2]
                     template.invalid_var_format_string = True
@@ -530,10 +581,10 @@
             ### EXCEPTIONS ############################################################
 
             # Raise exception for invalid template name
-            'exception01': ("{% extends 'nonexistent' %}", {}, template.TemplateSyntaxError),
+            'exception01': ("{% extends 'nonexistent' %}", {}, template.TemplateDoesNotExist),
 
             # Raise exception for invalid template name (in variable)
-            'exception02': ("{% extends nonexistent %}", {}, template.TemplateSyntaxError),
+            'exception02': ("{% extends nonexistent %}", {}, (template.TemplateSyntaxError, template.TemplateDoesNotExist)),
 
             # Raise exception for extra {% extends %} tags
             'exception03': ("{% extends 'inheritance01' %}{% block first %}2{% endblock %}{% extends 'inheritance16' %}", {}, template.TemplateSyntaxError),
Index: tests/regressiontests/templates/templates/test_extends_error.html
===================================================================
--- tests/regressiontests/templates/templates/test_extends_error.html	(revision 0)
+++ tests/regressiontests/templates/templates/test_extends_error.html	(revision 0)
@@ -0,0 +1 @@
+{% extends "broken_base.html" %}

Property changes on: tests/regressiontests/templates/templates/test_extends_error.html
___________________________________________________________________
Added: svn:executable
   + *
Added: svn:eol-style
   + native

Index: tests/regressiontests/templates/templates/broken_base.html
===================================================================
--- tests/regressiontests/templates/templates/broken_base.html	(revision 0)
+++ tests/regressiontests/templates/templates/broken_base.html	(revision 0)
@@ -0,0 +1 @@
+{% include "missing.html" %}

Property changes on: tests/regressiontests/templates/templates/broken_base.html
___________________________________________________________________
Added: svn:eol-style
   + native

