Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Template system now supports variables whose str() returns a Unicode …

…object with non-ascii characters. Thanks, gabor

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4161 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit be217bce53f4449708e20e2b71d9c8c4bf5b81eb 1 parent 460ccfc
Adrian Holovaty authored
6  django/template/__init__.py
@@ -742,7 +742,11 @@ def __repr__(self):
742 742
     def encode_output(self, output):
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)
748 752
         else:
18  tests/regressiontests/templates/tests.py
... ...
@@ -1,3 +1,4 @@
  1
+# -*- coding: utf-8 -*-
1 2
 from django.conf import settings
2 3
 
3 4
 if __name__ == '__main__':
@@ -62,6 +63,11 @@ class OtherClass:
62 63
     def method(self):
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):
67 73
         # NOW and NOW_tz are used by timesince tag tests.
@@ -173,6 +179,10 @@ 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'),
175 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'),
  185
+
176 186
             ### COMMENT SYNTAX ########################################################
177 187
             'comment-syntax01': ("{# this is hidden #}hello", {}, "hello"),
178 188
             'comment-syntax02': ("{# this is hidden #}hello{# foo #}", {}, "hello"),
@@ -328,18 +338,18 @@ def test_templates(self):
328 338
             'ifchanged05': ('{% for n in num %}{% ifchanged %}{{ n }}{% endifchanged %}{% for x in numx %}{% ifchanged %}{{ x }}{% endifchanged %}{% endfor %}{% endfor %}', { 'num': (1, 1, 1), 'numx': (1, 2, 3)}, '1123123123'),
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.
345 355
             'ifchanged-param04': ('{% for d in days %}{% ifchanged d.day %}{{ d.day }}{% endifchanged %}{% for h in d.hours %}{% ifchanged d.day h %}{{ h }}{% endifchanged %}{% endfor %}{% endfor %}', {'days':[{'day':1, 'hours':[1,2,3]},{'day':2, 'hours':[3]},] }, '112323'),

0 notes on commit be217bc

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