Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #14046 -- Made {% include %} behave the same, regardless of whe…

…ther the template included is named by variable or constant string. Thanks to defcube for the report, and George Karpenkov for the draft patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@15413 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit d8c38bd2c988a08cbb89af09f7eff2c0fade99d9 1 parent 1a2f9ff
Russell Keith-Magee authored February 04, 2011
1  AUTHORS
@@ -258,6 +258,7 @@ answer newbie questions, and generally made Django that much better:
258 258
     Bahadır Kandemir <bahadir@pardus.org.tr>
259 259
     Karderio <karderio@gmail.com>
260 260
     Nagy Károly <charlie@rendszergazda.com>
  261
+    George Karpenkov <george@metaworld.ru>
261 262
     Erik Karulf <erik@karulf.com>
262 263
     Ben Dean Kawamura <ben.dean.kawamura@gmail.com>
263 264
     Ian G. Kelly <ian.g.kelly@gmail.com>
4  django/template/loader_tags.py
@@ -168,12 +168,10 @@ def render(self, context):
168 168
             template_name = self.template_name.resolve(context)
169 169
             template = get_template(template_name)
170 170
             return self.render_template(template, context)
171  
-        except TemplateSyntaxError:
  171
+        except:
172 172
             if settings.TEMPLATE_DEBUG:
173 173
                 raise
174 174
             return ''
175  
-        except:
176  
-            return '' # Fail silently for invalid included templates.
177 175
 
178 176
 def do_block(parser, token):
179 177
     """
7  tests/regressiontests/templates/templatetags/bad_tag.py
... ...
@@ -0,0 +1,7 @@
  1
+from django import template
  2
+
  3
+register = template.Library()
  4
+
  5
+@register.tag
  6
+def badtag(parser, token):
  7
+    raise RuntimeError("I am a bad tag")
2  tests/regressiontests/templates/templatetags/broken_tag.py
... ...
@@ -1 +1 @@
1  
-from django import Xtemplate
  1
+from django import Xtemplate
68  tests/regressiontests/templates/tests.py
@@ -389,46 +389,59 @@ def test_template_loader(template_name, template_dirs=None):
389 389
             if isinstance(vals[2], tuple):
390 390
                 normal_string_result = vals[2][0]
391 391
                 invalid_string_result = vals[2][1]
392  
-                if isinstance(invalid_string_result, basestring) and '%s' in invalid_string_result:
  392
+
  393
+                if isinstance(invalid_string_result, tuple):
393 394
                     expected_invalid_str = 'INVALID %s'
394  
-                    invalid_string_result = invalid_string_result % vals[2][2]
  395
+                    invalid_string_result = invalid_string_result[0] % invalid_string_result[1]
395 396
                     template_base.invalid_var_format_string = True
  397
+
  398
+                try:
  399
+                    template_debug_result = vals[2][2]
  400
+                except IndexError:
  401
+                    template_debug_result = normal_string_result
  402
+
396 403
             else:
397 404
                 normal_string_result = vals[2]
398 405
                 invalid_string_result = vals[2]
  406
+                template_debug_result = vals[2]
399 407
 
400 408
             if 'LANGUAGE_CODE' in vals[1]:
401 409
                 activate(vals[1]['LANGUAGE_CODE'])
402 410
             else:
403 411
                 activate('en-us')
404 412
 
405  
-            for invalid_str, result in [('', normal_string_result),
406  
-                                        (expected_invalid_str, invalid_string_result)]:
  413
+            for invalid_str, template_debug, result in [
  414
+                    ('', False, normal_string_result),
  415
+                    (expected_invalid_str, False, invalid_string_result),
  416
+                    ('', True, template_debug_result)
  417
+                ]:
407 418
                 settings.TEMPLATE_STRING_IF_INVALID = invalid_str
  419
+                settings.TEMPLATE_DEBUG = template_debug
408 420
                 for is_cached in (False, True):
409 421
                     try:
410 422
                         start = datetime.now()
411 423
                         test_template = loader.get_template(name)
412 424
                         end = datetime.now()
413 425
                         if end-start > timedelta(seconds=0.2):
414  
-                            failures.append("Template test (Cached='%s', TEMPLATE_STRING_IF_INVALID='%s'): %s -- FAILED. Took too long to parse test" % (is_cached, invalid_str, name))
  426
+                            failures.append("Template test (Cached='%s', TEMPLATE_STRING_IF_INVALID='%s', TEMPLATE_DEBUG=%s): %s -- FAILED. Took too long to parse test" % (is_cached, invalid_str, template_debug, name))
415 427
 
416 428
                         start = datetime.now()
417 429
                         output = self.render(test_template, vals)
418 430
                         end = datetime.now()
419 431
                         if end-start > timedelta(seconds=0.2):
420  
-                            failures.append("Template test (Cached='%s', TEMPLATE_STRING_IF_INVALID='%s'): %s -- FAILED. Took too long to render test" % (is_cached, invalid_str, name))
  432
+                            failures.append("Template test (Cached='%s', TEMPLATE_STRING_IF_INVALID='%s', TEMPLATE_DEBUG=%s): %s -- FAILED. Took too long to render test" % (is_cached, invalid_str, template_debug, name))
421 433
                     except ContextStackException:
422  
-                        failures.append("Template test (Cached='%s', TEMPLATE_STRING_IF_INVALID='%s'): %s -- FAILED. Context stack was left imbalanced" % (is_cached, invalid_str, name))
  434
+                        failures.append("Template test (Cached='%s', TEMPLATE_STRING_IF_INVALID='%s', TEMPLATE_DEBUG=%s): %s -- FAILED. Context stack was left imbalanced" % (is_cached, invalid_str, template_debug, name))
423 435
                         continue
424 436
                     except Exception:
425 437
                         exc_type, exc_value, exc_tb = sys.exc_info()
426 438
                         if exc_type != result:
  439
+                            print "CHECK", name, exc_type, result
427 440
                             tb = '\n'.join(traceback.format_exception(exc_type, exc_value, exc_tb))
428  
-                            failures.append("Template test (Cached='%s', TEMPLATE_STRING_IF_INVALID='%s'): %s -- FAILED. Got %s, exception: %s\n%s" % (is_cached, invalid_str, name, exc_type, exc_value, tb))
  441
+                            failures.append("Template test (Cached='%s', TEMPLATE_STRING_IF_INVALID='%s', TEMPLATE_DEBUG=%s): %s -- FAILED. Got %s, exception: %s\n%s" % (is_cached, invalid_str, template_debug, name, exc_type, exc_value, tb))
429 442
                         continue
430 443
                     if output != result:
431  
-                        failures.append("Template test (Cached='%s', TEMPLATE_STRING_IF_INVALID='%s'): %s -- FAILED. Expected %r, got %r" % (is_cached, invalid_str, name, result, output))
  444
+                        failures.append("Template test (Cached='%s', TEMPLATE_STRING_IF_INVALID='%s', TEMPLATE_DEBUG=%s): %s -- FAILED. Expected %r, got %r" % (is_cached, invalid_str, template_debug, name, result, output))
432 445
                 cache_loader.reset()
433 446
 
434 447
             if 'LANGUAGE_CODE' in vals[1]:
@@ -616,7 +629,7 @@ def get_template_tests(self):
616 629
 
617 630
             # In methods that raise an exception without a
618 631
             # "silent_variable_attribute" set to True, the exception propagates
619  
-            'filter-syntax14': (r'1{{ var.method4 }}2', {"var": SomeClass()}, SomeOtherException),
  632
+            'filter-syntax14': (r'1{{ var.method4 }}2', {"var": SomeClass()}, (SomeOtherException, SomeOtherException, template.TemplateSyntaxError)),
620 633
 
621 634
             # Escaped backslash in argument
622 635
             'filter-syntax15': (r'{{ var|default_if_none:"foo\bar" }}', {"var": None}, r'foo\bar'),
@@ -645,8 +658,8 @@ def get_template_tests(self):
645 658
             # In attribute and dict lookups that raise an unexpected exception
646 659
             # without a "silent_variable_attribute" set to True, the exception
647 660
             # propagates
648  
-            'filter-syntax23': (r'1{{ var.noisy_fail_key }}2', {"var": SomeClass()}, SomeOtherException),
649  
-            'filter-syntax24': (r'1{{ var.noisy_fail_attribute }}2', {"var": SomeClass()}, SomeOtherException),
  661
+            'filter-syntax23': (r'1{{ var.noisy_fail_key }}2', {"var": SomeClass()}, (SomeOtherException, SomeOtherException, template.TemplateSyntaxError)),
  662
+            'filter-syntax24': (r'1{{ var.noisy_fail_attribute }}2', {"var": SomeClass()}, (SomeOtherException, SomeOtherException, template.TemplateSyntaxError)),
650 663
 
651 664
             ### COMMENT SYNTAX ########################################################
652 665
             'comment-syntax01': ("{# this is hidden #}hello", {}, "hello"),
@@ -699,7 +712,7 @@ def get_template_tests(self):
699 712
             ### EXCEPTIONS ############################################################
700 713
 
701 714
             # Raise exception for invalid template name
702  
-            'exception01': ("{% extends 'nonexistent' %}", {}, template.TemplateDoesNotExist),
  715
+            'exception01': ("{% extends 'nonexistent' %}", {}, (template.TemplateDoesNotExist, template.TemplateDoesNotExist, template.TemplateSyntaxError)),
703 716
 
704 717
             # Raise exception for invalid template name (in variable)
705 718
             'exception02': ("{% extends nonexistent %}", {}, (template.TemplateSyntaxError, template.TemplateDoesNotExist)),
@@ -968,7 +981,7 @@ def get_template_tests(self):
968 981
             'include01': ('{% include "basic-syntax01" %}', {}, "something cool"),
969 982
             'include02': ('{% include "basic-syntax02" %}', {'headline': 'Included'}, "Included"),
970 983
             'include03': ('{% include template_name %}', {'template_name': 'basic-syntax02', 'headline': 'Included'}, "Included"),
971  
-            'include04': ('a{% include "nonexistent" %}b', {}, "ab"),
  984
+            'include04': ('a{% include "nonexistent" %}b', {}, ("ab", "ab", template.TemplateDoesNotExist)),
972 985
             'include 05': ('template with a space', {}, 'template with a space'),
973 986
             'include06': ('{% include "include 05"%}', {}, 'template with a space'),
974 987
 
@@ -989,6 +1002,15 @@ def get_template_tests(self):
989 1002
             'include-error05': ('{% include "basic-syntax01" foo="duplicate" foo="key" %}', {}, template.TemplateSyntaxError),
990 1003
             'include-error06': ('{% include "basic-syntax01" only only %}', {}, template.TemplateSyntaxError),
991 1004
 
  1005
+            ### INCLUSION ERROR REPORTING #############################################
  1006
+            'include-fail1': ('{% load bad_tag %}{% badtag %}', {}, RuntimeError),
  1007
+            'include-fail2': ('{% load broken_tag %}', {}, template.TemplateSyntaxError),
  1008
+            'include-error07': ('{% include "include-fail1" %}', {}, ('', '', RuntimeError)),
  1009
+            'include-error08': ('{% include "include-fail2" %}', {}, ('', '', template.TemplateSyntaxError)),
  1010
+            'include-error09': ('{% include failed_include %}', {'failed_include': 'include-fail1'}, ('', '', template.TemplateSyntaxError)),
  1011
+            'include-error10': ('{% include failed_include %}', {'failed_include': 'include-fail2'}, ('', '', template.TemplateSyntaxError)),
  1012
+
  1013
+
992 1014
             ### NAMED ENDBLOCKS #######################################################
993 1015
 
994 1016
             # Basic test
@@ -1230,8 +1252,8 @@ def get_template_tests(self):
1230 1252
             'invalidstr03': ('{% for v in var %}({{ v }}){% endfor %}', {}, ''),
1231 1253
             'invalidstr04': ('{% if var %}Yes{% else %}No{% endif %}', {}, 'No'),
1232 1254
             'invalidstr04': ('{% if var|default:"Foo" %}Yes{% else %}No{% endif %}', {}, 'Yes'),
1233  
-            'invalidstr05': ('{{ var }}', {}, ('', 'INVALID %s', 'var')),
1234  
-            'invalidstr06': ('{{ var.prop }}', {'var': {}}, ('', 'INVALID %s', 'var.prop')),
  1255
+            'invalidstr05': ('{{ var }}', {}, ('', ('INVALID %s', 'var'))),
  1256
+            'invalidstr06': ('{{ var.prop }}', {'var': {}}, ('', ('INVALID %s', 'var.prop'))),
1235 1257
 
1236 1258
             ### MULTILINE #############################################################
1237 1259
 
@@ -1393,8 +1415,8 @@ def get_template_tests(self):
1393 1415
 
1394 1416
             # Failures
1395 1417
             'old-url-fail01': ('{% url %}', {}, template.TemplateSyntaxError),
1396  
-            'old-url-fail02': ('{% url no_such_view %}', {}, urlresolvers.NoReverseMatch),
1397  
-            'old-url-fail03': ('{% url regressiontests.templates.views.client %}', {}, urlresolvers.NoReverseMatch),
  1418
+            'old-url-fail02': ('{% url no_such_view %}', {}, (urlresolvers.NoReverseMatch, urlresolvers.NoReverseMatch, template.TemplateSyntaxError)),
  1419
+            'old-url-fail03': ('{% url regressiontests.templates.views.client %}', {}, (urlresolvers.NoReverseMatch, urlresolvers.NoReverseMatch, template.TemplateSyntaxError)),
1398 1420
             'old-url-fail04': ('{% url view id, %}', {}, template.TemplateSyntaxError),
1399 1421
             'old-url-fail05': ('{% url view id= %}', {}, template.TemplateSyntaxError),
1400 1422
             'old-url-fail06': ('{% url view a.id=id %}', {}, template.TemplateSyntaxError),
@@ -1433,8 +1455,8 @@ def get_template_tests(self):
1433 1455
 
1434 1456
             # Failures
1435 1457
             'url-fail01': ('{% load url from future %}{% url %}', {}, template.TemplateSyntaxError),
1436  
-            'url-fail02': ('{% load url from future %}{% url "no_such_view" %}', {}, urlresolvers.NoReverseMatch),
1437  
-            'url-fail03': ('{% load url from future %}{% url "regressiontests.templates.views.client" %}', {}, urlresolvers.NoReverseMatch),
  1458
+            'url-fail02': ('{% load url from future %}{% url "no_such_view" %}', {}, (urlresolvers.NoReverseMatch, urlresolvers.NoReverseMatch, template.TemplateSyntaxError)),
  1459
+            'url-fail03': ('{% load url from future %}{% url "regressiontests.templates.views.client" %}', {}, (urlresolvers.NoReverseMatch, urlresolvers.NoReverseMatch, template.TemplateSyntaxError)),
1438 1460
             'url-fail04': ('{% load url from future %}{% url "view" id, %}', {}, template.TemplateSyntaxError),
1439 1461
             'url-fail05': ('{% load url from future %}{% url "view" id= %}', {}, template.TemplateSyntaxError),
1440 1462
             'url-fail06': ('{% load url from future %}{% url "view" a.id=id %}', {}, template.TemplateSyntaxError),
@@ -1442,9 +1464,9 @@ def get_template_tests(self):
1442 1464
             'url-fail08': ('{% load url from future %}{% url "view" id="unterminatedstring %}', {}, template.TemplateSyntaxError),
1443 1465
             'url-fail09': ('{% load url from future %}{% url "view" id=", %}', {}, template.TemplateSyntaxError),
1444 1466
 
1445  
-            'url-fail11': ('{% load url from future %}{% url named_url %}', {}, urlresolvers.NoReverseMatch),
1446  
-            'url-fail12': ('{% load url from future %}{% url named_url %}', {'named_url': 'no_such_view'}, urlresolvers.NoReverseMatch),
1447  
-            'url-fail13': ('{% load url from future %}{% url named_url %}', {'named_url': 'regressiontests.templates.views.client'}, urlresolvers.NoReverseMatch),
  1467
+            'url-fail11': ('{% load url from future %}{% url named_url %}', {}, (urlresolvers.NoReverseMatch, urlresolvers.NoReverseMatch, template.TemplateSyntaxError)),
  1468
+            'url-fail12': ('{% load url from future %}{% url named_url %}', {'named_url': 'no_such_view'}, (urlresolvers.NoReverseMatch, urlresolvers.NoReverseMatch, template.TemplateSyntaxError)),
  1469
+            'url-fail13': ('{% load url from future %}{% url named_url %}', {'named_url': 'regressiontests.templates.views.client'}, (urlresolvers.NoReverseMatch, urlresolvers.NoReverseMatch, template.TemplateSyntaxError)),
1448 1470
             'url-fail14': ('{% load url from future %}{% url named_url id, %}', {'named_url': 'view'}, template.TemplateSyntaxError),
1449 1471
             'url-fail15': ('{% load url from future %}{% url named_url id= %}', {'named_url': 'view'}, template.TemplateSyntaxError),
1450 1472
             'url-fail16': ('{% load url from future %}{% url named_url a.id=id %}', {'named_url': 'view'}, template.TemplateSyntaxError),

0 notes on commit d8c38bd

Please sign in to comment.
Something went wrong with that request. Please try again.