Python 如何获取从django.template导入的RequestContext字典
我的django项目中有以下代码Python 如何获取从django.template导入的RequestContext字典,python,django,Python,Django,我的django项目中有以下代码 ctxt = RequestContext(request, { 'power': power, 'attack': attack, 'defense': defense, }) 现在我想通过ctxt { 'power': power, 'attack': attack, 'defense': defense, } 我尝试了ctxt.dicts,但其中包含的项目太多。因此,我深入研究了源代码,并在c
ctxt = RequestContext(request, {
'power': power,
'attack': attack,
'defense': defense,
})
现在我想通过ctxt
{
'power': power,
'attack': attack,
'defense': defense,
}
我尝试了ctxt.dicts
,但其中包含的项目太多。因此,我深入研究了源代码,并在classrequestcontext(Context):
我想这会带来其他的东西
那我怎么才能得到呢
顺便说一句,如果你想知道我为什么要这样做,你可以看看我以前问过的这个问题
ctxt.dict是一堆字典,因此您只需:
ctxt.dict[0]
我参加聚会有点晚了,但是您可以使用
ctxt.flatten()
如果您使用的是Django>=1.7 ctxt.dicts[0]在django 1.11中工作。使用“dict”会生成错误:AttributeError:“RequestContext”对象没有属性“dict”ctxt。Flatte()是一种更好的方法。但是,此解决方案不只是检索上下文的一个子部分吗?如果您是从Django标记提取上下文;然后使用
选择_模板
或获取_模板
;然后使用flattetemplate.render(context.flatte())
ctxt.flatten()