Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #3753 -- Allow optional display of invalid variable name in

TEMPLATE_STRING_IF_INVALID. Thanks, Matt McClanahan.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@5167 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit faa31732caa17d942b05fd4e25c49bb0589ce76f 1 parent 77ec8ae
Malcolm Tredinnick authored May 08, 2007
2  AUTHORS
@@ -147,7 +147,7 @@ answer newbie questions, and generally made Django that much better:
147 147
     lerouxb@gmail.com
148 148
     Waylan Limberg <waylan@gmail.com>
149 149
     limodou
150  
-    mattmcc
  150
+    Matt McClanahan <http://mmcc.cx/>
151 151
     Martin Maney <http://www.chipy.org/Martin_Maney>
152 152
     masonsimon+django@gmail.com
153 153
     Manuzhai
9  django/template/__init__.py
@@ -99,6 +99,10 @@
99 99
 # global list of libraries to load by default for a new parser
100 100
 builtins = []
101 101
 
  102
+# True if TEMPLATE_STRING_IF_INVALID contains a format string (%s). None means
  103
+# uninitialised.
  104
+invalid_var_format_string = None
  105
+
102 106
 class TemplateSyntaxError(Exception):
103 107
     def __str__(self):
104 108
         try:
@@ -575,6 +579,11 @@ def resolve(self, context, ignore_failures=False):
575 579
                 obj = None
576 580
             else:
577 581
                 if settings.TEMPLATE_STRING_IF_INVALID:
  582
+                    global invalid_var_format_string
  583
+                    if invalid_var_format_string is None:
  584
+                        invalid_var_format_string = '%s' in settings.TEMPLATE_STRING_IF_INVALID
  585
+                    if invalid_var_format_string:
  586
+                        return settings.TEMPLATE_STRING_IF_INVALID % self.var
578 587
                     return settings.TEMPLATE_STRING_IF_INVALID
579 588
                 else:
580 589
                     obj = settings.TEMPLATE_STRING_IF_INVALID
19  docs/templates_python.txt
@@ -212,21 +212,24 @@ template tags. If an invalid variable is provided to one of these template
212 212
 tags, the variable will be interpreted as ``None``. Filters are always
213 213
 applied to invalid variables within these template tags.
214 214
 
  215
+If ``TEMPLATE_STRING_IF_INVALID`` contains a ``'%s'``, the format marker will
  216
+be replaced with the name of the invalid variable.
  217
+
215 218
 .. admonition:: For debug purposes only!
216 219
 
217  
-    While ``TEMPLATE_STRING_IF_INVALID`` can be a useful debugging tool, 
218  
-    it is a bad idea to turn it on as a 'development default'. 
  220
+    While ``TEMPLATE_STRING_IF_INVALID`` can be a useful debugging tool,
  221
+    it is a bad idea to turn it on as a 'development default'.
219 222
     
220  
-    Many templates, including those in the Admin site, rely upon the 
221  
-    silence of the template system when a non-existent variable is 
  223
+    Many templates, including those in the Admin site, rely upon the
  224
+    silence of the template system when a non-existent variable is
222 225
     encountered. If you assign a value other than ``''`` to
223  
-    ``TEMPLATE_STRING_IF_INVALID``, you will experience rendering 
  226
+    ``TEMPLATE_STRING_IF_INVALID``, you will experience rendering
224 227
     problems with these templates and sites.
225 228
     
226  
-    Generally, ``TEMPLATE_STRING_IF_INVALID`` should only be enabled 
227  
-    in order to debug a specific template problem, then cleared 
  229
+    Generally, ``TEMPLATE_STRING_IF_INVALID`` should only be enabled
  230
+    in order to debug a specific template problem, then cleared
228 231
     once debugging is complete.
229  
-        
  232
+
230 233
 Playing with Context objects
231 234
 ----------------------------
232 235
 
13  tests/regressiontests/templates/tests.py
@@ -586,6 +586,8 @@ def test_templates(self):
586 586
             'invalidstr03': ('{% for v in var %}({{ v }}){% endfor %}', {}, ''),
587 587
             'invalidstr04': ('{% if var %}Yes{% else %}No{% endif %}', {}, 'No'),
588 588
             'invalidstr04': ('{% if var|default:"Foo" %}Yes{% else %}No{% endif %}', {}, 'Yes'),
  589
+            'invalidstr05': ('{{ var }}', {}, ('', 'INVALID %s', 'var')),
  590
+            'invalidstr06': ('{{ var.prop }}', {'var': {}}, ('', 'INVALID %s', 'var.prop')),
589 591
 
590 592
             ### MULTILINE #############################################################
591 593
 
@@ -737,6 +739,7 @@ def test_template_loader(template_name, template_dirs=None):
737 739
 
738 740
         # Set TEMPLATE_STRING_IF_INVALID to a known string
739 741
         old_invalid = settings.TEMPLATE_STRING_IF_INVALID
  742
+        expected_invalid_str = 'INVALID'
740 743
 
741 744
         for name, vals in tests:
742 745
             install()
@@ -744,6 +747,10 @@ def test_template_loader(template_name, template_dirs=None):
744 747
             if isinstance(vals[2], tuple):
745 748
                 normal_string_result = vals[2][0]
746 749
                 invalid_string_result = vals[2][1]
  750
+                if '%s' in invalid_string_result:
  751
+                    expected_invalid_str = 'INVALID %s'
  752
+                    invalid_string_result = invalid_string_result % vals[2][2]
  753
+                    template.invalid_var_format_string = True
747 754
             else:
748 755
                 normal_string_result = vals[2]
749 756
                 invalid_string_result = vals[2]
@@ -754,7 +761,7 @@ def test_template_loader(template_name, template_dirs=None):
754 761
                 activate('en-us')
755 762
 
756 763
             for invalid_str, result in [('', normal_string_result),
757  
-                                        ('INVALID', invalid_string_result)]:
  764
+                                        (expected_invalid_str, invalid_string_result)]:
758 765
                 settings.TEMPLATE_STRING_IF_INVALID = invalid_str
759 766
                 try:
760 767
                     output = loader.get_template(name).render(template.Context(vals[1]))
@@ -768,6 +775,10 @@ def test_template_loader(template_name, template_dirs=None):
768 775
             if 'LANGUAGE_CODE' in vals[1]:
769 776
                 deactivate()
770 777
 
  778
+            if template.invalid_var_format_string:
  779
+                expected_invalid_str = 'INVALID'
  780
+                template.invalid_var_format_string = False
  781
+
771 782
         loader.template_source_loaders = old_template_loaders
772 783
         deactivate()
773 784
         settings.TEMPLATE_DEBUG = old_td

0 notes on commit faa3173

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