3

有没有办法在运行测试工具时获取返回的 ContextList 对象的键?

如果我有:

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

有没有办法循环遍历键一、二和三?

4

1 回答 1

6
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
于 2012-08-23T17:33:23.690 回答