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

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

Fixes #15849 -- use context.render_context instead of self for storing state in IfChangedNode. Also now use self instead of str(id(self)) as the key when storing the node in contextforloop?`. Patch is against r16046.

  • django/template/defaulttags.py

    diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py
    index f4018ab..c5944a2 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
    245         self._id = str(id(self))
    246244
    247245    def render(self, context):
    248         if 'forloop' in context and self._id not in context['forloop']:
    249             self._last_seen = None
    250             context['forloop'][self._id] = 1
     246        if self not in context.render_context:
     247            context.render_context[self] = None  # last seen
     248        if 'forloop' in context and self not in context['forloop']:
     249            context.render_context[self] = None  # last seen
     250            context['forloop'][self] = 1
    251251        try:
    252252            if self._varlist:
    253253                # Consider multiple parameters.  This automatically behaves
    class IfChangedNode(Node): 
    258258        except VariableDoesNotExist:
    259259            compare_to = None
    260260
    261         if compare_to != self._last_seen:
    262             firstloop = (self._last_seen == None)
    263             self._last_seen = compare_to
     261        if compare_to != context.render_context[self]:  # last seen
     262            firstloop = (context.render_context[self] == None)
     263            context.render_context[self] = compare_to
    264264            content = self.nodelist_true.render(context)
    265265            return content
    266266        elif self.nodelist_false:
Back to Top