diff --git a/django/contrib/sitemaps/views.py b/django/contrib/sitemaps/views.py index d615c8e661..86bcc151d9 100644 --- a/django/contrib/sitemaps/views.py +++ b/django/contrib/sitemaps/views.py @@ -26,5 +26,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}) + xml = loader.render_to_string('sitemap.xml', {'urlset': urls}, encoding='utf-8') return HttpResponse(xml, mimetype='application/xml') diff --git a/django/template/__init__.py b/django/template/__init__.py index b9349e64cd..97a3c9d182 100644 --- a/django/template/__init__.py +++ b/django/template/__init__.py @@ -176,9 +176,9 @@ class Template(object): for subnode in node: yield subnode - def render(self, context): + def render(self, context, encoding=None): "Display stage -- can be called many times" - return self.nodelist.render(context) + return self.nodelist.render(context, encoding) def compile_string(template_string, origin): "Compiles template_string into NodeList ready for rendering" @@ -730,14 +730,15 @@ class NodeList(list): # data. codec_errors = 'replace' - def render(self, context): + def render(self, context, encoding=None): + if encoding is None: + encoding = settings.DEFAULT_CHARSET bits = [] for node in self: if isinstance(node, Node): bits.append(self.render_node(node, context)) else: bits.append(node) - encoding = settings.DEFAULT_CHARSET return ''.join([smart_str(b, encoding, errors=self.codec_errors) for b in bits]) def get_nodes_by_type(self, nodetype): diff --git a/django/template/loader.py b/django/template/loader.py index 03e6f8d49d..d142e8951e 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): +def render_to_string(template_name, dictionary=None, context_instance=None, encoding=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): context_instance.update(dictionary) else: context_instance = Context(dictionary) - return t.render(context_instance) + return t.render(context_instance, encoding) 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 aa2b8321be..b18b178820 100644 --- a/django/test/utils.py +++ b/django/test/utils.py @@ -10,10 +10,10 @@ from django.template import Template # the test database. TEST_DATABASE_PREFIX = 'test_' -def instrumented_test_render(self, context): - """An instrumented Template render method, providing a signal +def instrumented_test_render(self, context, unused=None): + """ + An instrumented Template render method, providing a signal that can be intercepted by the test system Client - """ dispatcher.send(signal=signals.template_rendered, sender=self, template=self, context=context) return self.nodelist.render(context)