[Django] #35417: RequestContext.new creates a context that cannot be flattened

11 views
Skip to first unread message

Django

unread,
Apr 30, 2024, 11:06:44 AMApr 30
to django-...@googlegroups.com
#35417: RequestContext.new creates a context that cannot be flattened
-----------------------------------------+------------------------
Reporter: Lily Foote | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 5.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
In
[https://github.com/django/django/blob/c187f5f9242b681abaa199173e02066997439425/django/template/library.py#L273
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
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35417>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 30, 2024, 11:09:11 AMApr 30
to django-...@googlegroups.com
#35417: RequestContext.new creates a context that cannot be flattened
-------------------------------+--------------------------------------
Reporter: Lily Foote | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 5.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Description changed by Lily Foote:

Old description:
New description:
This is similar to #24765 and #26041.

--
--
Ticket URL: <https://code.djangoproject.com/ticket/35417#comment:1>

Django

unread,
Apr 30, 2024, 2:19:11 PMApr 30
to django-...@googlegroups.com
#35417: RequestContext.new creates a context that cannot be flattened
-------------------------------+--------------------------------------
Reporter: Lily Foote | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 5.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Comment (by Lily Foote):

Investigating a bit more, this also applies to {{{Context}}} and
{{{RenderContext}}}.
--
Ticket URL: <https://code.djangoproject.com/ticket/35417#comment:2>

Django

unread,
May 2, 2024, 3:52:37 AMMay 2
to django-...@googlegroups.com
#35417: RequestContext.new creates a context that cannot be flattened
-------------------------------+------------------------------------
Reporter: Lily Foote | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 5.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by Sarah Boyce):

* stage: Unreviewed => Accepted
* type: Uncategorized => Bug

Comment:

Replicated (thank you for the test case) and also confirmed `Context` and
`RenderContext`, thank you!
--
Ticket URL: <https://code.djangoproject.com/ticket/35417#comment:3>

Django

unread,
May 2, 2024, 3:56:30 AMMay 2
to django-...@googlegroups.com
#35417: RequestContext.new creates a context that cannot be flattened
---------------------------------+------------------------------------
Reporter: Lily Foote | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 5.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by Sarah Boyce):

* component: Uncategorized => Template system

--
Ticket URL: <https://code.djangoproject.com/ticket/35417#comment:4>
Reply all
Reply to author
Forward
0 new messages