Skip to content

Commit 52b0051

Browse files
committed
unicode: Added (optional) explicit template encoding specification. Also ported
contrib.sitemaps over (we want to ensure the output XML is in a valid encoding, so we force UTF-8 in this case). git-svn-id: http://code.djangoproject.com/svn/django/branches/unicode@5277 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent a8404e6 commit 52b0051

File tree

4 files changed

+11
-10
lines changed

4 files changed

+11
-10
lines changed

django/contrib/sitemaps/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ def sitemap(request, sitemaps, section=None):
2626
urls.extend(site().get_urls())
2727
else:
2828
urls.extend(site.get_urls())
29-
xml = loader.render_to_string('sitemap.xml', {'urlset': urls})
29+
xml = loader.render_to_string('sitemap.xml', {'urlset': urls}, encoding='utf-8')
3030
return HttpResponse(xml, mimetype='application/xml')

django/template/__init__.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ def __iter__(self):
176176
for subnode in node:
177177
yield subnode
178178

179-
def render(self, context):
179+
def render(self, context, encoding=None):
180180
"Display stage -- can be called many times"
181-
return self.nodelist.render(context)
181+
return self.nodelist.render(context, encoding)
182182

183183
def compile_string(template_string, origin):
184184
"Compiles template_string into NodeList ready for rendering"
@@ -730,14 +730,15 @@ class NodeList(list):
730730
# data.
731731
codec_errors = 'replace'
732732

733-
def render(self, context):
733+
def render(self, context, encoding=None):
734+
if encoding is None:
735+
encoding = settings.DEFAULT_CHARSET
734736
bits = []
735737
for node in self:
736738
if isinstance(node, Node):
737739
bits.append(self.render_node(node, context))
738740
else:
739741
bits.append(node)
740-
encoding = settings.DEFAULT_CHARSET
741742
return ''.join([smart_str(b, encoding, errors=self.codec_errors) for b in bits])
742743

743744
def get_nodes_by_type(self, nodetype):

django/template/loader.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def get_template_from_string(source, origin=None, name=None):
8787
"""
8888
return Template(source, origin, name)
8989

90-
def render_to_string(template_name, dictionary=None, context_instance=None):
90+
def render_to_string(template_name, dictionary=None, context_instance=None, encoding=None):
9191
"""
9292
Loads the given template_name and renders it with the given dictionary as
9393
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):
103103
context_instance.update(dictionary)
104104
else:
105105
context_instance = Context(dictionary)
106-
return t.render(context_instance)
106+
return t.render(context_instance, encoding)
107107

108108
def select_template(template_name_list):
109109
"Given a list of template names, returns the first that can be loaded."

django/test/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
# the test database.
1111
TEST_DATABASE_PREFIX = 'test_'
1212

13-
def instrumented_test_render(self, context):
14-
"""An instrumented Template render method, providing a signal
13+
def instrumented_test_render(self, context, unused=None):
14+
"""
15+
An instrumented Template render method, providing a signal
1516
that can be intercepted by the test system Client
16-
1717
"""
1818
dispatcher.send(signal=signals.template_rendered, sender=self, template=self, context=context)
1919
return self.nodelist.render(context)

0 commit comments

Comments
 (0)