﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
24765	Calling flatten on a Context object fails in some cases	Dan Poirier	Buddy Lindsey	"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
"	Bug	closed	Template system	1.8	Normal	fixed			Accepted	1	0	0	1	0	0
