Search code examples
pythondjangodjango-testing

Django ContextList keys


Is there a way to get the keys of a returned ContextList object when running a test harness?

If I have:

return render_to_response('x.html', 
                              {
                               'one' : 1,
                               'two' : 2,
                               'three' : 3
                               },
                              context_instance=RequestContext(request)) 

Is there a way to loop through the keys one, two, & three?


Solution

  • keys = []
    for subcontext in response.context:
      for d in subcontext.dicts:
        for k in d.keys():
          if k not in keys:
            keys.append(k)
    
    print keys