如何在Django中编辑上下文(Context)以便渲染前?
来自TEMPLATE_CONTEXT_PROCESSORS的处理器不能编辑上下文,还是我搞错了?
即使我去继承
class MyContext(Context):
def __init__(self, *args, **kwargs):
Context.__init__(self, *args, **kwargs)
上下文并尝试把它传递给context_instance,它也不给我访问字典的权限。它是在渲染之前直接添加的。
return render_to_response(template, {'hello':'Hello World'}, context_instance=MyContext())
那么我该如何在渲染之前分析和编辑上下文呢?
更新:
我找到的唯一方法是继承Context并重写update方法。
class RequestContext(Context):
def update(self,other_dict):
print other_dict
super(RequestContext, self).update(other_dict)