Opened 7 weeks ago

Closed 7 days ago

Last modified 6 days ago

#35417 closed Bug (fixed)

RequestContext.new creates a context that cannot be flattened

Reported by: Lily Foote Owned by: George Kussumoto
Component: Template system Version: 5.0
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Lily Foote)

In InclusionNode.render Django creates a new_context from two existing contexts (context and _dict) by calling new_context = context.new(_dict). These can both be instances of RequestContext leading to new_context also being a RequestContext (I have not tried with any other context types). However, calling new_context.flatten() raises a ValueError:

ValueError: dictionary update sequence element #0 has length 1; 2 is required

I can reproduce this in a small test:

from django.template.context import RequestContext
from django.test import RequestFactory, TestCase


class RequestContextTestCase(TestCase):
    def test_flatten_request_context_new(self):
        factory = RequestFactory()

        request = factory.get("/foo/")
        context = RequestContext(request)
        context_2 = RequestContext(request)
        context_3 = context.new(context_2)

        self.assertEqual(
            context_3.flatten(), {"False": False, "None": None, "True": True}
        )

I discovered this when running Kolo on a Django admin view (http://127.0.0.1:8000/admin/auth/user/). Kolo calls context.flatten() internally when introspecting a template during rendering, which leads to this exception:

Traceback (most recent call last):
  File "/home/lily/work/kloppindustries/kolo/python/src/kolo/profiler.py", line 170, in __call__
    frame_data = processor.process(
                 ^^^^^^^^^^^^^^^^^^
  File "/home/lily/work/kloppindustries/kolo/python/src/kolo/plugins.py", line 107, in process
    data.update(self.process_extra(frame, event, arg, self.context))
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/lily/work/kloppindustries/kolo/python/src/kolo/filters/django.py", line 89, in process_django_template
    template_context = template_context.flatten()
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/lily/.local/share/lilyenv/virtualenvs/kolo-sandbox/3.12/lib/python3.12/site-packages/django/template/context.py", line 120, in flatten
    flat.update(d)
ValueError: dictionary update sequence element #0 has length 3; 2 is required

This is similar to #24765 and #26041.

Change History (9)

comment:1 by Lily Foote, 7 weeks ago

Description: modified (diff)

comment:2 by Lily Foote, 7 weeks ago

Investigating a bit more, this also applies to Context and RenderContext.

comment:3 by Sarah Boyce, 7 weeks ago

Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

Replicated (thank you for the test case) and also confirmed Context and RenderContext, thank you!

comment:4 by Sarah Boyce, 7 weeks ago

Component: UncategorizedTemplate system

comment:5 by George Kussumoto, 2 weeks ago

Owner: changed from nobody to George Kussumoto
Status: newassigned

comment:6 by George Kussumoto, 2 weeks ago

Has patch: set

PR: https://github.com/django/django/pull/18221

Hi, it's my first contribution here, so let me know if I failed to follow the guidelines.

in reply to:  6 comment:7 by Sarah Boyce, 7 days ago

Triage Stage: AcceptedReady for checkin

Replying to George Kussumoto:

Hi, it's my first contribution here, so let me know if I failed to follow the guidelines.

You did great 🥳

comment:8 by Sarah Boyce <42296566+sarahboyce@…>, 7 days ago

Resolution: fixed
Status: assignedclosed

In 2a32b233:

Fixed #35417 -- Updated BaseContext.new() with values to create a context that can be flattened.

comment:9 by Sarah Boyce <42296566+sarahboyce@…>, 6 days ago

In 64443f5:

[5.1.x] Fixed #35417 -- Updated BaseContext.new() with values to create a context that can be flattened.

Backport of 2a32b233822683c51e59722b7c9aa0789fc4ab1b from main.

Note: See TracTickets for help on using tickets.
Back to Top