2012-03-06 44 views
0

当我从视图中返回render_to_response时,有没有办法总是包含csrfContext = RequestContext(request)?我注意到,根据用户是否登录,我的许多视图都显示不同的内容。我认为如果将它包含在每个视图中都会有所帮助,所以我不必总是将其添加进去。或者,您能否给我一个原因说明为什么这是糟糕的设计选择?在每个render_to_response内包含csrf上下文?

回答

0

您需要包含context_instance,这段代码取自djandgo文档。

return render_to_response('my_template.html', 
         my_data_dictionary, 
         context_instance=RequestContext(request)) 

通知的context_instance=RequestContext(request),你需要这个被你的回应返回。

0

使用render

ctx = {'foo':1} 
return render(request,'index.html',ctx) 
+0

我需要使用render_to_response,因为我需要传递值的字典以显示在模板上。 – egidra 2012-03-06 05:51:48

+0

请阅读文档。 – 2012-03-06 05:52:13