Django源码学习 了解render与render_to_response
2024-10-10 08:18:04
render与render_to_response
def render_to_response(template_name, context=None, content_type=None, status=None, using=None):
content = loader.render_to_string(template_name, context, using=using)
return HttpResponse(content, content_type, status)
def render(request, template_name, context=None, content_type=None, status=None, using=None):
content = loader.render_to_string(template_name, context, request, using=using)
return HttpResponse(content, content_type, status)
这两个快捷函数都是内部调用了django.template.loader.render_to_string函数,
返回的HTML内容文本给response。
这两个函数之间的区别就在于是否传入request对象,其实传入的context上下文最终传入render_to_string中,然后获取选择模板对象,然后模板对象调用其render函数,再次传入context上下文对象,插入模板返回一系列HTML文本字符串。
最后这些字符串向外传递给(feed into)HTTPResponse对象,快捷返回的是HTTPResponse对象。
render_to_string中有两个重要的函数被调用就是get_template和select_template,通过模板引擎来获取模板对象。
最新文章
- UI第十九节——UICollectionView
- 转载:AbstractQueuedSynchronizer的介绍和原理分析
- dojo grid 编辑问题
- 【转载】谷歌酝酿将苹果Swift作为安卓APP主要开发语言
- [转]关于jquery中html()、text()、val()的区别
- JS适配问题。
- c# App.Config详解
- eclipse下导入工程的小问题
- DirectX 绘制
- 在Android上使用Google V8 JS 引擎
- Fragment inner class should be static
- ASP.NET 4.0 来了
- iOS 获取项目名称及版本号
- git:hook declined FATAL: W refs/heads DENIED by fallthru error
- C / C++算法学习笔记(8)-SHELL排序
- JavaScript for in的缺陷
- [LeetCode] Score of Parentheses 括号的分数
- #6 ipdb模块源代码解读
- 劳德巴赫下载kernel和文件系统时问题
- 4面向对象(OOP)
热门文章
- 用NIO实现http协议
- nginx反向代理cas server之1:多个cas server负载均衡配置以及ssl配置
- 常见浏览器的宽高代码写法!有原生JavaScript和jquery两种写法-------------------------------以及我的个人网站
- centos5.5下mangodb启动报错glibc
- .NET Framework 各个版本介绍
- mysql 发生系统错误 1067
- 【CC2530入门教程-03】CC2530的中断系统及外部中断应用
- Streaming结合Kafka
- Spring Boot 集成swagger实例
- 分享一款简单好用的HTML拼接工具