Code

Ticket #15849: 15849-ifchanged-not-thread-safe.1.diff

File 15849-ifchanged-not-thread-safe.1.diff, 1.4 KB (added by akaihola, 3 years ago)

Fixes #15849 -- use context.render_context instead of self for storing state in IfChangedNode

Line 
1diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py
2index f4018ab..d2b0cbd 100644
3--- a/django/template/defaulttags.py
4+++ b/django/template/defaulttags.py
5@@ -240,13 +240,14 @@ class IfChangedNode(Node):
6 
7     def __init__(self, nodelist_true, nodelist_false, *varlist):
8         self.nodelist_true, self.nodelist_false = nodelist_true, nodelist_false
9-        self._last_seen = None
10         self._varlist = varlist
11         self._id = str(id(self))
12 
13     def render(self, context):
14+        if self._id not in context.render_context:
15+            context.render_context[self._id] = None  # last seen
16         if 'forloop' in context and self._id not in context['forloop']:
17-            self._last_seen = None
18+            context.render_context[self._id] = None  # last seen
19             context['forloop'][self._id] = 1
20         try:
21             if self._varlist:
22@@ -258,9 +259,9 @@ class IfChangedNode(Node):
23         except VariableDoesNotExist:
24             compare_to = None
25 
26-        if compare_to != self._last_seen:
27-            firstloop = (self._last_seen == None)
28-            self._last_seen = compare_to
29+        if compare_to != context.render_context[self._id]:  # last seen
30+            firstloop = (context.render_context[self._id] == None)
31+            context.render_context[self._id] = compare_to
32             content = self.nodelist_true.render(context)
33             return content
34         elif self.nodelist_false: