mirror of
https://github.com/django/django.git
synced 2025-07-04 09:49:12 +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:
parent
a8404e66fd
commit
52b0051523
@ -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')
|
||||
|
@ -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):
|
||||
|
@ -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."
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user