1
0
mirror of https://github.com/django/django.git synced 2025-07-04 17:59:13 +00:00

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
This commit is contained in:
Malcolm Tredinnick 2007-05-17 16:55:38 +00:00
parent a8404e66fd
commit 52b0051523
4 changed files with 11 additions and 10 deletions

View File

@ -26,5 +26,5 @@ def sitemap(request, sitemaps, section=None):
urls.extend(site().get_urls()) urls.extend(site().get_urls())
else: else:
urls.extend(site.get_urls()) 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') return HttpResponse(xml, mimetype='application/xml')

View File

@ -176,9 +176,9 @@ class Template(object):
for subnode in node: for subnode in node:
yield subnode yield subnode
def render(self, context): def render(self, context, encoding=None):
"Display stage -- can be called many times" "Display stage -- can be called many times"
return self.nodelist.render(context) return self.nodelist.render(context, encoding)
def compile_string(template_string, origin): def compile_string(template_string, origin):
"Compiles template_string into NodeList ready for rendering" "Compiles template_string into NodeList ready for rendering"
@ -730,14 +730,15 @@ class NodeList(list):
# data. # data.
codec_errors = 'replace' codec_errors = 'replace'
def render(self, context): def render(self, context, encoding=None):
if encoding is None:
encoding = settings.DEFAULT_CHARSET
bits = [] bits = []
for node in self: for node in self:
if isinstance(node, Node): if isinstance(node, Node):
bits.append(self.render_node(node, context)) bits.append(self.render_node(node, context))
else: else:
bits.append(node) bits.append(node)
encoding = settings.DEFAULT_CHARSET
return ''.join([smart_str(b, encoding, errors=self.codec_errors) for b in bits]) return ''.join([smart_str(b, encoding, errors=self.codec_errors) for b in bits])
def get_nodes_by_type(self, nodetype): def get_nodes_by_type(self, nodetype):

View File

@ -87,7 +87,7 @@ def get_template_from_string(source, origin=None, name=None):
""" """
return Template(source, origin, name) 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 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. 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) context_instance.update(dictionary)
else: else:
context_instance = Context(dictionary) context_instance = Context(dictionary)
return t.render(context_instance) return t.render(context_instance, encoding)
def select_template(template_name_list): def select_template(template_name_list):
"Given a list of template names, returns the first that can be loaded." "Given a list of template names, returns the first that can be loaded."

View File

@ -10,10 +10,10 @@ from django.template import Template
# the test database. # the test database.
TEST_DATABASE_PREFIX = 'test_' TEST_DATABASE_PREFIX = 'test_'
def instrumented_test_render(self, context): def instrumented_test_render(self, context, unused=None):
"""An instrumented Template render method, providing a signal """
An instrumented Template render method, providing a signal
that can be intercepted by the test system Client that can be intercepted by the test system Client
""" """
dispatcher.send(signal=signals.template_rendered, sender=self, template=self, context=context) dispatcher.send(signal=signals.template_rendered, sender=self, template=self, context=context)
return self.nodelist.render(context) return self.nodelist.render(context)