Code

Ticket #12787: 12787.2.diff

File 12787.2.diff, 11.1 KB (added by kmtracey, 4 years ago)
Line 
1Index: django/template/loaders/cached.py
2===================================================================
3--- django/template/loaders/cached.py   (revision 12791)
4+++ django/template/loaders/cached.py   (working copy)
5@@ -37,7 +37,10 @@
6         if template_name not in self.template_cache:
7             template, origin = self.find_template(template_name, template_dirs)
8             if not hasattr(template, 'render'):
9-                template = get_template_from_string(template, origin, template_name)
10+                try:
11+                    template = get_template_from_string(template, origin, template_name)
12+                except TemplateDoesNotExist:
13+                    return template, origin
14             self.template_cache[template_name] = template
15         return self.template_cache[template_name], None
16 
17Index: django/template/loader_tags.py
18===================================================================
19--- django/template/loader_tags.py      (revision 12791)
20+++ django/template/loader_tags.py      (working copy)
21@@ -97,10 +97,7 @@
22             raise TemplateSyntaxError(error_msg)
23         if hasattr(parent, 'render'):
24             return parent # parent is a Template object
25-        try:
26-            return get_template(parent)
27-        except TemplateDoesNotExist:
28-            raise TemplateSyntaxError("Template %r cannot be extended, because it doesn't exist" % parent)
29+        return get_template(parent)
30 
31     def render(self, context):
32         compiled_parent = self.get_parent(context)
33Index: django/template/loader.py
34===================================================================
35--- django/template/loader.py   (revision 12791)
36+++ django/template/loader.py   (working copy)
37@@ -44,8 +44,11 @@
38     def load_template(self, template_name, template_dirs=None):
39         source, display_name = self.load_template_source(template_name, template_dirs)
40         origin = make_origin(display_name, self.load_template_source, template_name, template_dirs)
41-        template = get_template_from_string(source, origin, template_name)
42-        return template, None
43+        try:
44+            template = get_template_from_string(source, origin, template_name)
45+            return template, None
46+        except TemplateDoesNotExist:
47+            return source, display_name
48 
49     def load_template_source(self, template_name, template_dirs=None):
50         """
51Index: tests/regressiontests/templates/tests.py
52===================================================================
53--- tests/regressiontests/templates/tests.py    (revision 12791)
54+++ tests/regressiontests/templates/tests.py    (working copy)
55@@ -160,39 +160,90 @@
56         # Turn TEMPLATE_DEBUG on, so that the origin file name will be kept with
57         # the compiled templates.
58         old_td, settings.TEMPLATE_DEBUG = settings.TEMPLATE_DEBUG, True
59+        old_loaders = loader.template_source_loaders
60 
61+        try:
62+            loader.template_source_loaders = (filesystem.Loader(),)
63+
64+            # We rely on the fact that runtests.py sets up TEMPLATE_DIRS to
65+            # point to a directory containing a 404.html file. Also that
66+            # the file system and app directories loaders both inherit the
67+            # load_template method from the BaseLoader class, so we only need
68+            # to test one of them.
69+            load_name = '404.html'
70+            template = loader.get_template(load_name)
71+            template_name = template.nodelist[0].source[0].name
72+            self.assertTrue(template_name.endswith(load_name),
73+                'Template loaded by filesystem loader has incorrect name for debug page: %s' % template_name)
74+
75+            # Aso test the cached loader, since it overrides load_template
76+            cache_loader = cached.Loader(('',))
77+            cache_loader._cached_loaders = loader.template_source_loaders
78+            loader.template_source_loaders = (cache_loader,)
79+
80+            template = loader.get_template(load_name)
81+            template_name = template.nodelist[0].source[0].name
82+            self.assertTrue(template_name.endswith(load_name),
83+                'Template loaded through cached loader has incorrect name for debug page: %s' % template_name)
84+
85+            template = loader.get_template(load_name)
86+            template_name = template.nodelist[0].source[0].name
87+            self.assertTrue(template_name.endswith(load_name),
88+                'Cached template loaded through cached loader has incorrect name for debug page: %s' % template_name)
89+        finally:
90+            loader.template_source_loaders = old_loaders
91+            settings.TEMPLATE_DEBUG = old_td
92+
93+    def test_does_not_exist_name_app(self):
94+        old_td, settings.TEMPLATE_DEBUG = settings.TEMPLATE_DEBUG, True
95         old_loaders = loader.template_source_loaders
96-        loader.template_source_loaders = (filesystem.Loader(),)
97 
98-        # We rely on the fact that runtests.py sets up TEMPLATE_DIRS to
99-        # point to a directory containing a 404.html file. Also that
100-        # the file system and app directories loaders both inherit the
101-        # load_template method from the BaseLoader class, so we only need
102-        # to test one of them.
103-        load_name = '404.html'
104-        template = loader.get_template(load_name)
105-        template_name = template.nodelist[0].source[0].name
106-        self.assertTrue(template_name.endswith(load_name),
107-            'Template loaded by filesystem loader has incorrect name for debug page: %s' % template_name)
108+        try:
109+            loader.template_source_loaders = (app_directories.Loader(),)
110 
111-        # Aso test the cached loader, since it overrides load_template
112-        cache_loader = cached.Loader(('',))
113-        cache_loader._cached_loaders = loader.template_source_loaders
114-        loader.template_source_loaders = (cache_loader,)
115+            load_name = 'test_extends_error.html'
116+            tmpl = loader.get_template(load_name)
117+            r = None
118+            try:
119+                r = tmpl.render(template.Context({}))
120+            except template.TemplateSyntaxError, e:
121+                settings.TEMPLATE_DEBUG = old_td
122+                self.assertEqual(e.args[0], 'Caught TemplateDoesNotExist while rendering: missing.html')
123+            self.assertEqual(r, None, 'Template rendering unexpectedly succeeded, produced: ->%r<-' % r)
124+        finally:
125+            loader.template_source_loaders = old_loaders
126+            settings.TEMPLATE_DEBUG = old_td
127 
128-        template = loader.get_template(load_name)
129-        template_name = template.nodelist[0].source[0].name
130-        self.assertTrue(template_name.endswith(load_name),
131-            'Template loaded through cached loader has incorrect name for debug page: %s' % template_name)
132+    def test_does_not_exist_name_cache(self):
133+        old_td, settings.TEMPLATE_DEBUG = settings.TEMPLATE_DEBUG, True
134+        old_loaders = loader.template_source_loaders
135 
136-        template = loader.get_template(load_name)
137-        template_name = template.nodelist[0].source[0].name
138-        self.assertTrue(template_name.endswith(load_name),
139-            'Cached template loaded through cached loader has incorrect name for debug page: %s' % template_name)
140+        try:
141+            cache_loader = cached.Loader(('',))
142+            cache_loader._cached_loaders = (app_directories.Loader(),)
143+            loader.template_source_loaders = (cache_loader,)
144 
145-        loader.template_source_loaders = old_loaders
146-        settings.TEMPLATE_DEBUG = old_td
147+            load_name = 'test_extends_error.html'
148+            tmpl = loader.get_template(load_name)
149+            r = None
150+            try:
151+                r = tmpl.render(template.Context({}))
152+            except template.TemplateSyntaxError, e:
153+                self.assertEqual(e.args[0], 'Caught TemplateDoesNotExist while rendering: missing.html')
154+            self.assertEqual(r, None, 'Template rendering unexpectedly succeeded, produced: ->%r<-' % r)
155 
156+            # For the cached loader, repeat the test, to ensure the first attempt did not cache a
157+            # result that behaves incorrectly on subsequent attempts.
158+            tmpl = loader.get_template(load_name)
159+            try:
160+                tmpl.render(template.Context({}))
161+            except template.TemplateSyntaxError, e:
162+                self.assertEqual(e.args[0], 'Caught TemplateDoesNotExist while rendering: missing.html')
163+            self.assertEqual(r, None, 'Template rendering unexpectedly succeeded, produced: ->%r<-' % r)
164+        finally:
165+            loader.template_source_loaders = old_loaders
166+            settings.TEMPLATE_DEBUG = old_td
167+
168     def test_token_smart_split(self):
169         # Regression test for #7027
170         token = template.Token(template.TOKEN_BLOCK, 'sometag _("Page not found") value|yesno:_("yes,no")')
171@@ -269,7 +320,7 @@
172             if isinstance(vals[2], tuple):
173                 normal_string_result = vals[2][0]
174                 invalid_string_result = vals[2][1]
175-                if '%s' in invalid_string_result:
176+                if isinstance(invalid_string_result, basestring) and '%s' in invalid_string_result:
177                     expected_invalid_str = 'INVALID %s'
178                     invalid_string_result = invalid_string_result % vals[2][2]
179                     template.invalid_var_format_string = True
180@@ -530,10 +581,10 @@
181             ### EXCEPTIONS ############################################################
182 
183             # Raise exception for invalid template name
184-            'exception01': ("{% extends 'nonexistent' %}", {}, template.TemplateSyntaxError),
185+            'exception01': ("{% extends 'nonexistent' %}", {}, template.TemplateDoesNotExist),
186 
187             # Raise exception for invalid template name (in variable)
188-            'exception02': ("{% extends nonexistent %}", {}, template.TemplateSyntaxError),
189+            'exception02': ("{% extends nonexistent %}", {}, (template.TemplateSyntaxError, template.TemplateDoesNotExist)),
190 
191             # Raise exception for extra {% extends %} tags
192             'exception03': ("{% extends 'inheritance01' %}{% block first %}2{% endblock %}{% extends 'inheritance16' %}", {}, template.TemplateSyntaxError),
193Index: tests/regressiontests/templates/templates/test_extends_error.html
194===================================================================
195--- tests/regressiontests/templates/templates/test_extends_error.html   (revision 0)
196+++ tests/regressiontests/templates/templates/test_extends_error.html   (revision 0)
197@@ -0,0 +1 @@
198+{% extends "broken_base.html" %}
199
200Property changes on: tests/regressiontests/templates/templates/test_extends_error.html
201___________________________________________________________________
202Added: svn:executable
203   + *
204Added: svn:eol-style
205   + native
206
207Index: tests/regressiontests/templates/templates/broken_base.html
208===================================================================
209--- tests/regressiontests/templates/templates/broken_base.html  (revision 0)
210+++ tests/regressiontests/templates/templates/broken_base.html  (revision 0)
211@@ -0,0 +1 @@
212+{% include "missing.html" %}
213
214Property changes on: tests/regressiontests/templates/templates/broken_base.html
215___________________________________________________________________
216Added: svn:eol-style
217   + native
218