<commit_before>from contextlib import contextmanager
import functools
import re
from django.template.response import TemplateResponse
from django.template import TemplateSyntaxError, NodeList
from django.template.loader_tags import BlockNode
from django.views.decorators.vary import vary_on_headers


class PJAXBlockNodeList(NodeList):
    """
    When using the pjax_block decorator, matching blocks' nodelists are set
    to this class. The overridden render method stores the block's output in
    the render context for later retrieval, before returning as normal.
    """

    def render(self, context):
        result = super(PJAXBlockNodeList, self).render(context)
        context.render_context["pjax_captured_blocks"][self.block_name] = result
        return result


class PJAXBlockTemplateResponse(TemplateResponse):

    @property
    def rendered_content(self):
        """
        Walk the template's node tree, casting our target blocks' nodelists to
        PJAXBlockNodeList in order to store its output in the render context.
        Then render the template as usual, but instead of returning the result,
        return the captured output from our target block(s).
        """
        template = self.resolve_template(self.template_name)
        context = self.resolve_context(self.context_data)

        captured_blocks = dict()
        context.render_context["pjax_captured_blocks"] = captured_blocks<commit_msg>Don't use render_context to store captured contents.<commit_after>from contextlib import contextmanager
import functools
import re
from django.template.response import TemplateResponse
from django.template import TemplateSyntaxError, NodeList
from django.template.loader_tags import BlockNode
from django.views.decorators.vary import vary_on_headers


class PJAXBlockNodeList(NodeList):
    """
    When using the pjax_block decorator, matching blocks' nodelists are set
    to this class. The overridden render method stores the block's output in
    the render context for later retrieval, before returning as normal.
    """

    def render(self, context):
        result = super(PJAXBlockNodeList, self).render(context)
        if '_pjax_captured_blocks' in context.__dict__:
            # If using the cached template loader, this method can be called
            # even for non-PJAX requests, in which case capture nothing.
            context._pjax_captured_blocks[self.block_name] = result
        return result


class PJAXBlockTemplateResponse(TemplateResponse):

    @property
    def rendered_content(self):
        """
        Walk the template's node tree, casting our target blocks' nodelists to
        PJAXBlockNodeList in order to store its output in the render context.
        Then render the template as usual, but instead of returning the result,
        return the captured output from our target block(s).
        """
        template = self.resolve_template(self.template_name)
        context = self.resolve_context(self.context_data)

        captured_blocks = dict()
        context._pjax_captured_blocks = captured_blocks
{'fraction_non_alphanumeric': 0.05834683954619125, 'fraction_numerical': 0.0, 'mean_word_length': 3.6406015037593984, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3561784', 'n_tokens_mistral': 791, 'n_tokens_neox': 766, 'n_words': 277}