Django

Code

Changeset 5167

Show
Ignore:
Timestamp:
05/07/07 22:36:16 (1 year ago)
Author:
mtredinnick
Message:

Fixed #3753 -- Allow optional display of invalid variable name in
TEMPLATE_STRING_IF_INVALID. Thanks, Matt McClanahan?.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/AUTHORS

    r5166 r5167  
    148148    Waylan Limberg <waylan@gmail.com> 
    149149    limodou 
    150     mattmcc 
     150    Matt McClanahan <http://mmcc.cx/> 
    151151    Martin Maney <http://www.chipy.org/Martin_Maney> 
    152152    masonsimon+django@gmail.com 
  • django/trunk/django/template/__init__.py

    r5117 r5167  
    9999# global list of libraries to load by default for a new parser 
    100100builtins = [] 
     101 
     102# True if TEMPLATE_STRING_IF_INVALID contains a format string (%s). None means 
     103# uninitialised. 
     104invalid_var_format_string = None 
    101105 
    102106class TemplateSyntaxError(Exception): 
     
    576580            else: 
    577581                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 
    578587                    return settings.TEMPLATE_STRING_IF_INVALID 
    579588                else: 
  • django/trunk/docs/templates_python.txt

    r5161 r5167  
    213213applied to invalid variables within these template tags. 
    214214 
     215If ``TEMPLATE_STRING_IF_INVALID`` contains a ``'%s'``, the format marker will 
     216be replaced with the name of the invalid variable. 
     217 
    215218.. admonition:: For debug purposes only! 
    216219 
    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'. 
    219222     
    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 
    222225    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 
    224227    problems with these templates and sites. 
    225228     
    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 
    228231    once debugging is complete. 
    229          
     232 
    230233Playing with Context objects 
    231234---------------------------- 
  • django/trunk/tests/regressiontests/templates/tests.py

    r5125 r5167  
    587587            'invalidstr04': ('{% if var %}Yes{% else %}No{% endif %}', {}, 'No'), 
    588588            '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')), 
    589591 
    590592            ### MULTILINE ############################################################# 
     
    738740        # Set TEMPLATE_STRING_IF_INVALID to a known string 
    739741        old_invalid = settings.TEMPLATE_STRING_IF_INVALID 
     742        expected_invalid_str = 'INVALID' 
    740743 
    741744        for name, vals in tests: 
     
    745748                normal_string_result = vals[2][0] 
    746749                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 
    747754            else: 
    748755                normal_string_result = vals[2] 
     
    755762 
    756763            for invalid_str, result in [('', normal_string_result), 
    757                                         ('INVALID', invalid_string_result)]: 
     764                                        (expected_invalid_str, invalid_string_result)]: 
    758765                settings.TEMPLATE_STRING_IF_INVALID = invalid_str 
    759766                try: 
     
    769776                deactivate() 
    770777 
     778            if template.invalid_var_format_string: 
     779                expected_invalid_str = 'INVALID' 
     780                template.invalid_var_format_string = False 
     781 
    771782        loader.template_source_loaders = old_template_loaders 
    772783        deactivate()