#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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/050.3cb411a0e228a255278e7aaa531a6c62%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to