Changeset 4161
- Timestamp:
- 12/05/06 13:48:46 (2 years ago)
- Files:
-
- django/trunk/django/template/__init__.py (modified) (1 diff)
- django/trunk/tests/regressiontests/templates/tests.py (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/template/__init__.py
r4038 r4161 743 743 # Check type so that we don't run str() on a Unicode object 744 744 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) 746 750 elif isinstance(output, unicode): 747 751 return output.encode(settings.DEFAULT_CHARSET) django/trunk/tests/regressiontests/templates/tests.py
r4050 r4161 1 # -*- coding: utf-8 -*- 1 2 from django.conf import settings 2 3 … … 63 64 return "OtherClass.method" 64 65 66 class UnicodeInStrClass: 67 "Class whose __str__ returns a Unicode object." 68 def __str__(self): 69 return u'ŠĐĆŽćžšđ' 70 65 71 class Templates(unittest.TestCase): 66 72 def test_templates(self): … … 173 179 # Empty strings can be passed as arguments to filters 174 180 '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'), 175 185 176 186 ### COMMENT SYNTAX ######################################################## … … 329 339 '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'), 330 340 '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 332 342 # Test one parameter given to ifchanged. 333 343 'ifchanged-param01': ('{% for n in num %}{% ifchanged n %}..{% endifchanged %}{{ n }}{% endfor %}', { 'num': (1,2,3) }, '..1..2..3'), 334 344 '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 336 346 # Test multiple parameters to ifchanged. 337 347 '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 339 349 # Test a date+hour like construct, where the hour of the last day 340 350 # is the same but the date had changed, so print the hour anyway. 341 351 '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 343 353 # Logically the same as above, just written with explicit 344 354 # ifchanged for the day.
