diff options
author | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2007-06-17 10:25:48 +0000 |
---|---|---|
committer | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2007-06-17 10:25:48 +0000 |
commit | 2126d41aba06b50a288cc6a516d4531ced998822 (patch) | |
tree | 2ed0e56153471e6076903c99fda834a5f78dd364 | |
parent | 1bfcfe615c65565da5009a205aecff394217717c (diff) | |
download | django-2126d41aba06b50a288cc6a516d4531ced998822.tar.gz |
unicode: Render templates as unicode objects and only convert them to
bytestrings as part of HttpRespnose and other output paths.
git-svn-id: http://code.djangoproject.com/svn/django/branches/unicode@5487 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r-- | django/contrib/sitemaps/views.py | 3 | ||||
-rw-r--r-- | django/http/__init__.py | 4 | ||||
-rw-r--r-- | django/template/__init__.py | 17 | ||||
-rw-r--r-- | django/template/loader.py | 4 | ||||
-rw-r--r-- | django/test/utils.py | 2 | ||||
-rw-r--r-- | tests/regressiontests/forms/tests.py | 2 | ||||
-rw-r--r-- | tests/regressiontests/templates/tests.py | 2 | ||||
-rw-r--r-- | tests/regressiontests/templates/unicode.py | 4 |
8 files changed, 15 insertions, 23 deletions
diff --git a/django/contrib/sitemaps/views.py b/django/contrib/sitemaps/views.py index 86bcc151d9..86ef1e3526 100644 --- a/django/contrib/sitemaps/views.py +++ b/django/contrib/sitemaps/views.py @@ -2,6 +2,7 @@ from django.http import HttpResponse, Http404 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() @@ -26,5 +27,5 @@ def sitemap(request, sitemaps, section=None): 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') diff --git a/django/http/__init__.py b/django/http/__init__.py index 834c3e625e..b5af9b6e6a 100644 --- a/django/http/__init__.py +++ b/django/http/__init__.py @@ -271,9 +271,7 @@ class HttpResponse(object): 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): diff --git a/django/template/__init__.py b/django/template/__init__.py index 5b8cdc3b1f..422820f905 100644 --- a/django/template/__init__.py +++ b/django/template/__init__.py @@ -60,7 +60,7 @@ from django.conf import settings 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') @@ -176,9 +176,9 @@ class Template(object): 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" @@ -732,21 +732,14 @@ class Node(object): 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" diff --git a/django/template/loader.py b/django/template/loader.py index d142e8951e..03e6f8d49d 100644 --- a/django/template/loader.py +++ b/django/template/loader.py @@ -87,7 +87,7 @@ def get_template_from_string(source, origin=None, name=None): """ 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 @@ -103,7 +103,7 @@ def render_to_string(template_name, dictionary=None, context_instance=None, enco 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." diff --git a/django/test/utils.py b/django/test/utils.py index b0f4a8e53d..a33395a6d8 100644 --- a/django/test/utils.py +++ b/django/test/utils.py @@ -10,7 +10,7 @@ from django.template import Template # 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 diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py index 4844267b43..a2e23b8b42 100644 --- a/tests/regressiontests/forms/tests.py +++ b/tests/regressiontests/forms/tests.py @@ -3358,7 +3358,7 @@ does not have help text, nothing will be output. <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. diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py index 349daaf337..b6615e1aa9 100644 --- a/tests/regressiontests/templates/tests.py +++ b/tests/regressiontests/templates/tests.py @@ -223,7 +223,7 @@ class Templates(unittest.TestCase): # 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"), diff --git a/tests/regressiontests/templates/unicode.py b/tests/regressiontests/templates/unicode.py index 2f07c87c19..efda11c2da 100644 --- a/tests/regressiontests/templates/unicode.py +++ b/tests/regressiontests/templates/unicode.py @@ -24,10 +24,10 @@ Contexts can be constructed from unicode or UTF-8 bytestrings. >>> 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'> """ |