Tornado-网页模版和静态文件

1.手工创建文件夹用于存放网页模板以及静态文件

Tornado-网页模版和静态文件_第1张图片
image.png

2.实现代码

demo.py

"""
    tornado web app对于网页模板的处理和静态文件的操作
    网页模板:html页面
    处理:定义html页面、渲染html页面,响应html页面[浏览器]
    静态资源:图片/js/css/字体...
    操作:配置静态资源、查询静态资源[html]、响应数据
"""

# 引入需要的模块
from tornado.web import Application, RequestHandler
from tornado.ioloop import IOLoop
from tornado.options import define, options, parse_command_line
from tornado.httpserver import HTTPServer
import os.path

# 定义变量
define("port", default=8000, help="默认端口8000")


# 创建视图类
class IndexHandler(RequestHandler):
    def get(self):
        msg = "hello,零"
        self.render("index.html", info=msg)


# 程序入口
if __name__ == '__main__':
    # 开始监听
    parse_command_line()
    app = Application(
        [
            (r'/', IndexHandler)
        ],

        # 项目配置信息
        # 网页模板
        template_path=os.path.join(os.path.dirname(__file__), "templates"),
        # 静态文件
        static_path=os.path.join(os.path.dirname(__file__), "static"),

        debug=True
    )

    # 部署
    server = HTTPServer(app)
    server.listen(options.port)

    # 轮询监听
    IOLoop.current().start()

index.html




    
    Title
    
    


hello

{{info}}

注意:

Tornado模板模块提供了一个叫作static_url的函数来生成static目录下文件的URL。

在index.html中static_url的调用的示例代码:

这个对static_url的调用生成了URL的值,并渲染输出类似下面的代码:

 

你可能感兴趣的:(Tornado-网页模版和静态文件)