diff options
| author | Keryn Knight <keryn@kerynknight.com> | 2021-06-07 12:56:50 +0100 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2021-06-11 12:22:06 +0200 |
| commit | 854e9b066850b9b4eb1171966e996322b2c16d27 (patch) | |
| tree | 966d56419980921b2c30d27d25df270a64bf3b67 /django/template/base.py | |
| parent | 2dfc1066a0d31c7e443e1c7b8590b101a645bf2d (diff) | |
Fixed #32824 -- Improved performance of NodeList.render().
This avoids the following:
- checking that each item in the nodelist is a subclass of Node,
- calling str() on the render_annotated() output, because it's
documented that Node.render() must return a string,
- calling mark_safe() on the output, when the value to be wrapped is
definitively known to be a string because the result of ''.join()
is always of that type,
- using an intermediate list to store each individual string.
Diffstat (limited to 'django/template/base.py')
| -rw-r--r-- | django/template/base.py | 13 |
1 files changed, 4 insertions, 9 deletions
diff --git a/django/template/base.py b/django/template/base.py index 1446cc865e..4972bd7c58 100644 --- a/django/template/base.py +++ b/django/template/base.py @@ -59,7 +59,7 @@ from django.template.context import BaseContext from django.utils.formats import localize from django.utils.html import conditional_escape, escape from django.utils.regex_helper import _lazy_re_compile -from django.utils.safestring import SafeData, mark_safe +from django.utils.safestring import SafeData, SafeString, mark_safe from django.utils.text import ( get_text_list, smart_split, unescape_string_literal, ) @@ -954,14 +954,9 @@ class NodeList(list): contains_nontext = False def render(self, context): - bits = [] - for node in self: - if isinstance(node, Node): - bit = node.render_annotated(context) - else: - bit = node - bits.append(str(bit)) - return mark_safe(''.join(bits)) + return SafeString(''.join([ + node.render_annotated(context) for node in self + ])) def get_nodes_by_type(self, nodetype): "Return a list of all nodes of the given type" |
