{{{
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.
* needs_better_patch: => 0
* needs_docs: => 0
* needs_tests: => 0
* stage: Unreviewed => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/24765#comment:1>
* status: new => assigned
* owner: nobody => BuddyL
--
Ticket URL: <https://code.djangoproject.com/ticket/24765#comment:2>
* 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>
* 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>