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

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

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

  • django/template/defaulttags.py

    diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py
    index f4018ab..d2b0cbd 100644
    a b class IfChangedNode(Node): 
    240240
    241241    def __init__(self, nodelist_true, nodelist_false, *varlist):
    242242        self.nodelist_true, self.nodelist_false = nodelist_true, nodelist_false
    243         self._last_seen = None
    244243        self._varlist = varlist
    245244        self._id = str(id(self))
    246245
    247246    def render(self, context):
     247        if self._id not in context.render_context:
     248            context.render_context[self._id] = None  # last seen
    248249        if 'forloop' in context and self._id not in context['forloop']:
    249             self._last_seen = None
     250            context.render_context[self._id] = None  # last seen
    250251            context['forloop'][self._id] = 1
    251252        try:
    252253            if self._varlist:
    class IfChangedNode(Node): 
    258259        except VariableDoesNotExist:
    259260            compare_to = None
    260261
    261         if compare_to != self._last_seen:
    262             firstloop = (self._last_seen == None)
    263             self._last_seen = compare_to
     262        if compare_to != context.render_context[self._id]:  # last seen
     263            firstloop = (context.render_context[self._id] == None)
     264            context.render_context[self._id] = compare_to
    264265            content = self.nodelist_true.render(context)
    265266            return content
    266267        elif self.nodelist_false:
Back to Top