认识tornado(五)
2024-09-04 12:51:46
Tornado 自带了模板系统,模板语法与 Django 差异不大。这里简单地介绍如何使用 Tornado 的模板系统。
首先是编写 URL 规则与 Handler:
01 class NowaMagicHandler(tornado.web.RequestHandler):
02 def get(self):
03 content = u'Welcome to NowaMagic.'
04 #self.write( content )
05 self.render("index.html")
06
07 def main():
08 tornado.options.parse_command_line()
09 application = tornado.web.Application([
10 (r"/", MainHandler),
11 (r"/nowamagic/", NowaMagicHandler),
12 ],**settings)
13 http_server = tornado.httpserver.HTTPServer(application)
14 http_server.listen(options.port)
15 tornado.ioloop.IOLoop.instance().start()
然后是 index.html
01 <html>
02 <head>
03 <title>{{ title }}</title>
04 </head>
05 <body>
06 <h1>{{ title }}</h1>
07 <ul>8 {% for item in items %}
09 <li>{{ escape(item) }}</li>
10 {% end %}
11 </ul>
12 </body>
13 </html>
文件包含也是用 {% include 'header.html' %} 这样的语法,和 Django 里是一样的。
还有就是对静态文件的处理,一般是建一个叫 static 的文件夹,然后把js,css,images分类放进去。当然在程序里也得写个 setting:
1 import os
2
3 settings = {
4 "static_path" : os.path.join(os.path.dirname(__file__), "static"),
5 "template_path" : os.path.join(os.path.dirname(__file__), "templates"),
6 "gzip" : True,
7 "debug" : True,
8 }
setting 里还制定了模板的路径。关于这个 setting,更多可以参考这篇文章里提到的:如何开启Tornado的调试模式。
就这样,Tornado 的模板就OK了。
认识tornado(一)
认识tornado(二)
认识tornado(三)
认识tornado(四)
认识tornado(五)
最新文章
- 类别(Category)与扩展(Extensions)
- knockoutJS学习笔记04:监控属性
- 例子:Execution Model Sample - 应用状态保存
- DFS/BFS Codeforces Round #301 (Div. 2) C. Ice Cave
- For each db / table
- 应用hexo(rss插件)
- C++ sizeof
- MP3/WAV 播放
- C++ extern ";C";,C与C++的区别
- 使用storyboard实现页面跳转,简单的数据传递
- RTSP调试代码
- TControl.GetDeviceContext会给图形控件建立新的坐标原点和建立新的剪裁区域
- Java学习之道:空指针错误求解救????????????
- 解决谷歌浏览器 chrome解决跨域(CORS)问题---chrome插件
- git客户端的安装及使用
- vue中引用swiper轮播插件
- jdk的安装过程
- iOS上传本地代码到git
- 可遇不可求的Question之MySQL系统变量interactive_timeout 与 wait_timeout 篇
- struts2:多业务方法的处理(动态调用,DMI)
热门文章
- 创建了几个String对象?
- hibernate 1 + N 问题解决
- how to remove untagged / none images
- C#创建COM组件供VB,PB,Delphi调用
- 在Docker中执行web应用
- js可以关闭android页面上的键盘输入法
- Android实现换肤功能(一)
- 数据结构(逻辑结构,物理结构,特点) C#多线程编程的同步也线程安全 C#多线程编程笔记 String 与 StringBuilder (StringBuffer) 数据结构与算法-初体验(极客专栏)
- 爬虫1:get请求的翻页及思考
- Spring MVC 框架搭建及具体解释