Django

Code

Changeset 4161

Show
Ignore:
Timestamp:
12/05/06 13:48:46 (2 years ago)
Author:
adrian
Message:

Template system now supports variables whose str() returns a Unicode object with non-ascii characters. Thanks, gabor

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/template/__init__.py

    r4038 r4161  
    743743        # Check type so that we don't run str() on a Unicode object 
    744744        if not isinstance(output, basestring): 
    745             return str(output) 
     745            try: 
     746                return str(output) 
     747            except UnicodeEncodeError: 
     748                # If __str__() returns a Unicode object, convert it to bytestring. 
     749                return unicode(output).encode(settings.DEFAULT_CHARSET) 
    746750        elif isinstance(output, unicode): 
    747751            return output.encode(settings.DEFAULT_CHARSET) 
  • django/trunk/tests/regressiontests/templates/tests.py

    r4050 r4161  
     1# -*- coding: utf-8 -*- 
    12from django.conf import settings 
    23 
     
    6364        return "OtherClass.method" 
    6465 
     66class UnicodeInStrClass: 
     67    "Class whose __str__ returns a Unicode object." 
     68    def __str__(self): 
     69        return u'ŠĐĆŽćžšđ' 
     70 
    6571class Templates(unittest.TestCase): 
    6672    def test_templates(self): 
     
    173179            # Empty strings can be passed as arguments to filters 
    174180            'basic-syntax36': (r'{{ var|join:"" }}', {'var': ['a', 'b', 'c']}, 'abc'), 
     181 
     182            # If a variable has a __str__() that returns a Unicode object, the value 
     183            # will be converted to a bytestring. 
     184            'basic-syntax37': (r'{{ var }}', {'var': UnicodeInStrClass()}, '\xc5\xa0\xc4\x90\xc4\x86\xc5\xbd\xc4\x87\xc5\xbe\xc5\xa1\xc4\x91'), 
    175185 
    176186            ### COMMENT SYNTAX ######################################################## 
     
    329339            'ifchanged06': ('{% for n in num %}{% ifchanged %}{{ n }}{% endifchanged %}{% for x in numx %}{% ifchanged %}{{ x }}{% endifchanged %}{% endfor %}{% endfor %}', { 'num': (1, 1, 1), 'numx': (2, 2, 2)}, '1222'), 
    330340            'ifchanged07': ('{% for n in num %}{% ifchanged %}{{ n }}{% endifchanged %}{% for x in numx %}{% ifchanged %}{{ x }}{% endifchanged %}{% for y in numy %}{% ifchanged %}{{ y }}{% endifchanged %}{% endfor %}{% endfor %}{% endfor %}', { 'num': (1, 1, 1), 'numx': (2, 2, 2), 'numy': (3, 3, 3)}, '1233323332333'), 
    331              
     341 
    332342            # Test one parameter given to ifchanged. 
    333343            'ifchanged-param01': ('{% for n in num %}{% ifchanged n %}..{% endifchanged %}{{ n }}{% endfor %}', { 'num': (1,2,3) }, '..1..2..3'), 
    334344            'ifchanged-param02': ('{% for n in num %}{% for x in numx %}{% ifchanged n %}..{% endifchanged %}{{ x }}{% endfor %}{% endfor %}', { 'num': (1,2,3), 'numx': (5,6,7) }, '..567..567..567'), 
    335              
     345 
    336346            # Test multiple parameters to ifchanged. 
    337347            'ifchanged-param03': ('{% for n in num %}{{ n }}{% for x in numx %}{% ifchanged x n %}{{ x }}{% endifchanged %}{% endfor %}{% endfor %}', { 'num': (1,1,2), 'numx': (5,6,6) }, '156156256'), 
    338              
     348 
    339349            # Test a date+hour like construct, where the hour of the last day 
    340350            # is the same but the date had changed, so print the hour anyway. 
    341351            'ifchanged-param04': ('{% for d in days %}{% ifchanged %}{{ d.day }}{% endifchanged %}{% for h in d.hours %}{% ifchanged d h %}{{ h }}{% endifchanged %}{% endfor %}{% endfor %}', {'days':[{'day':1, 'hours':[1,2,3]},{'day':2, 'hours':[3]},] }, '112323'), 
    342              
     352 
    343353            # Logically the same as above, just written with explicit 
    344354            # ifchanged for the day.