Commits

Anonymous committed a65beae

unicode: Render templates as unicode objects and only convert them to
bytestrings as part of HttpRespnose and other output paths.

  • Participants
  • Parent commits 221e6b2
  • Branches unicode

Comments (0)

Files changed (8)

File django/contrib/sitemaps/views.py

 from django.template import loader
 from django.contrib.sites.models import Site
 from django.core import urlresolvers
+from django.utils.encoding import smart_str
 
 def index(request, sitemaps):
     current_site = Site.objects.get_current()
             urls.extend(site().get_urls())
         else:
             urls.extend(site.get_urls())
-    xml = loader.render_to_string('sitemap.xml', {'urlset': urls}, encoding='utf-8')
+    xml = smart_str(loader.render_to_string('sitemap.xml', {'urlset': urls}))
     return HttpResponse(xml, mimetype='application/xml')

File django/http/__init__.py

         self.cookies[key]['max-age'] = 0
 
     def _get_content(self):
-        content = ''.join(self._container)
-        if isinstance(content, unicode):
-            content = content.encode(self._charset)
+        content = smart_str(''.join(self._container), self._charset)
         return content
 
     def _set_content(self, value):

File django/template/__init__.py

 from django.template.context import Context, RequestContext, ContextPopException
 from django.utils.functional import curry, Promise
 from django.utils.text import smart_split
-from django.utils.encoding import smart_unicode, smart_str, force_unicode
+from django.utils.encoding import smart_unicode, force_unicode
 from django.utils.translation import ugettext as _
 
 __all__ = ('Template', 'Context', 'RequestContext', 'compile_string')
             for subnode in node:
                 yield subnode
 
-    def render(self, context, encoding=None):
+    def render(self, context):
         "Display stage -- can be called many times"
-        return self.nodelist.render(context, encoding)
+        return self.nodelist.render(context)
 
 def compile_string(template_string, origin):
     "Compiles template_string into NodeList ready for rendering"
         return nodes
 
 class NodeList(list):
-    # How invalid encoding sequences are handled. The default 'strict' is not
-    # appropriate, because the framework is not in control of all the string
-    # data.
-    codec_errors = 'replace'
-
-    def render(self, context, encoding=None):
-        if encoding is None:
-            encoding = settings.DEFAULT_CHARSET
+    def render(self, context):
         bits = []
         for node in self:
             if isinstance(node, Node):
                 bits.append(self.render_node(node, context))
             else:
                 bits.append(node)
-        return ''.join([smart_str(b, encoding, errors=self.codec_errors) for b in bits])
+        return ''.join([force_unicode(b) for b in bits])
 
     def get_nodes_by_type(self, nodetype):
         "Return a list of all nodes of the given type"

File django/template/loader.py

     """
     return Template(source, origin, name)
 
-def render_to_string(template_name, dictionary=None, context_instance=None, encoding=None):
+def render_to_string(template_name, dictionary=None, context_instance=None):
     """
     Loads the given template_name and renders it with the given dictionary as
     context. The template_name may be a string to load a single template using
         context_instance.update(dictionary)
     else:
         context_instance = Context(dictionary)
-    return t.render(context_instance, encoding)
+    return t.render(context_instance)
 
 def select_template(template_name_list):
     "Given a list of template names, returns the first that can be loaded."

File django/test/utils.py

 # the test database.
 TEST_DATABASE_PREFIX = 'test_'
 
-def instrumented_test_render(self, context, unused=None):
+def instrumented_test_render(self, context):
     """
     An instrumented Template render method, providing a signal
     that can be intercepted by the test system Client

File tests/regressiontests/forms/tests.py

 <input type="submit" />
 </form>
 >>> Template('{{ form.password1.help_text }}').render(Context({'form': UserRegistration(auto_id=False)}))
-''
+u''
 
 The label_tag() method takes an optional attrs argument: a dictionary of HTML
 attributes to add to the <label> tag.

File tests/regressiontests/templates/tests.py

 
             # Make sure that any unicode strings are converted to bytestrings
             # in the final output.
-            'filter-syntax18': (r'{{ var }}', {'var': UTF8Class()}, '\xc5\xa0\xc4\x90\xc4\x86\xc5\xbd\xc4\x87\xc5\xbe\xc5\xa1\xc4\x91'),
+            'filter-syntax18': (r'{{ var }}', {'var': UTF8Class()}, u'\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111'),
 
             ### COMMENT SYNTAX ########################################################
             'comment-syntax01': ("{# this is hidden #}hello", {}, "hello"),

File tests/regressiontests/templates/unicode.py

 >>> c4 = Context({u'var': '\xc4\x90\xc4\x91'})
 
 Since both templates and all four contexts represent the same thing, they all
-render the same (and are returned as bytestrings).
+render the same (and are returned as unicode objects).
 
 >>> t1.render(c3) == t2.render(c3)
 True
 >>> type(t1.render(c3))
-<type 'str'>
+<type 'unicode'>
 """