django render_to_response

橙三吉。 提交于 2020-01-14 03:05:15

问题


I'm putting

...}, context_instance=RequestContext(request))

at the end of all my render_to_response's. I'm sure this is not right. Can anyone tell me when I should be using these?


回答1:


You are doing it "right". This means that all of the Context Processors will run on this view, and you will have access to all of the juicy bits in your template.

The other way to do this is to use direct_to_template, which saves you having to instantiate a RequestContext object, but has the same outcomes.




回答2:


If you are using Django 1.3 you can use the render() shortcut function so you don't have to explicitly write context_instance=RequestContext(request) for each view .

Combines a given template with a given context dictionary and returns an HttpResponse object with that rendered text.

render() is the same as a call to render_to_response() with a context_instance argument that forces the use of a RequestContext.



来源:https://stackoverflow.com/questions/5693784/django-render-to-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!