[Django] #24765: Calling flatten on a Context object fails in some cases

45 views
Skip to first unread message

Django

unread,
May 7, 2015, 12:06:12 PM5/7/15
to django-...@googlegroups.com
#24765: Calling flatten on a Context object fails in some cases
---------------------------------+--------------------
Reporter: poirier | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 1.8
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------
For example, if one of the "dicts" in the Context object is actually a
RequestContext. Here's the `flatten` method in Django 1.8.1 (from
django/template/context.py, line 101):

{{{
def flatten(self):
"""
Returns self.dicts as one dictionary
"""
flat = {}
for d in self.dicts:
flat.update(d)
return flat
}}}

The line `flat.update(d)` fails because a RequestContext doesn't provide
enough of a dictionary's interface to be used in an `update`, and you get
errors like:

{{{
File ".../lib/python2.7/site-packages/django/template/context.py",
line 107, in flatten
flat.update(d)
ValueError: dictionary update sequence element #0 has length 6; 2 is
required
}}}

It might be a little tricky to come up with a case where a Context ends up
with a RequestContext in it. I believe in my case it's happening when a
template uses an `inclusion_tag` type template tag that returns its
`context` argument, possibly modified, e.g.:

{{{
@register.inclusion_tag('template_fragment.html',
takes_context=True)
def some_tag(context):
context['thingy'] = 12
return context
}}}

The return value gets pushed onto the Context. During testing, the Context
ends up being returned in the list of Context objects in Response.context,
which is where I was trying to call flatten on it.

I don't know if the right fix is to make a RequestContext behave more like
a dictionary, or to avoid pushing RequestContext objects onto a Context,
or something else. I will probably work around for now by forcing the
return values from my inclusion_tags to be simple dictionaries.

Context.flatten:
https://docs.djangoproject.com/en/1.8/ref/templates/api/#django.template.Context.flatten

--
Ticket URL: <https://code.djangoproject.com/ticket/24765>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 8, 2015, 7:21:27 AM5/8/15
to django-...@googlegroups.com
#24765: Calling flatten on a Context object fails in some cases
---------------------------------+------------------------------------

Reporter: poirier | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 1.8
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 timgraham):

* needs_better_patch: => 0
* needs_docs: => 0
* needs_tests: => 0
* stage: Unreviewed => Accepted


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

Django

unread,
Sep 10, 2015, 12:30:57 PM9/10/15
to django-...@googlegroups.com
#24765: Calling flatten on a Context object fails in some cases
---------------------------------+------------------------------------
Reporter: poirier | Owner: BuddyL
Type: Bug | Status: assigned

Component: Template system | Version: 1.8
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 BuddyL):

* status: new => assigned
* owner: nobody => BuddyL


--
Ticket URL: <https://code.djangoproject.com/ticket/24765#comment:2>

Django

unread,
Sep 11, 2015, 9:07:41 AM9/11/15
to django-...@googlegroups.com
#24765: Calling flatten on a Context object fails in some cases
---------------------------------+------------------------------------
Reporter: poirier | Owner: BuddyL
Type: Bug | Status: assigned
Component: Template system | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by timgraham):

* needs_better_patch: 0 => 1
* has_patch: 0 => 1


Comment:

I left some comments for improvement on the
[https://github.com/django/django/pull/5264 pull request]. poirier, could
you check if it solves your problem? I'm not sure if we should add a
higher level test for the case presented in the ticket or not.

--
Ticket URL: <https://code.djangoproject.com/ticket/24765#comment:3>

Django

unread,
Sep 11, 2015, 2:53:01 PM9/11/15
to django-...@googlegroups.com
#24765: Calling flatten on a Context object fails in some cases
---------------------------------+------------------------------------
Reporter: poirier | Owner: BuddyL
Type: Bug | Status: closed

Component: Template system | Version: 1.8
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"ec704371e301c5450dd5fc012a30424a8f47b99b" ec70437]:
{{{
#!CommitTicketReference repository=""
revision="ec704371e301c5450dd5fc012a30424a8f47b99b"
Fixed #24765 -- Allowed template context updates to flatten a Context.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24765#comment:4>

Reply all
Reply to author
Forward
0 new messages