HttpResponse render render_to_response 三者的区别

HttpResponse("

Here's the text of the Web page.

") 服务器直接返回一段字符串给客户端浏览器
或者用它返回一个文件HttpResponse(json_str, content_type='application/json; charset=utf-8')

render render_to_response
这里的render()不是django.template的t.render(),而是最近Django版本新增的django.shortcuts.render(),类似django.shortcuts.render_to_response(),但这个render()是用来替代render_to_response(),
官方文档关于render的说明

官方文档关于render_to_response的说明提到,

This function preceded the introduction of render() and works similarly except that it doesn’t make the request available in the response.
It’s not recommended and is likely to be deprecated in the future.

不建议再用render_to_response,用render代替。
render默认要求带入request,虽然render_to_response也可以用RequestContext将request参数带入。

posted @ 2016-11-07 12:42  小风阵阵  阅读(1218)  评论(0编辑  收藏  举报