tornado跳转到html页面,Tornado-网页模版和静态文件

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

759fcef9cb43

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的值,并渲染输出类似下面的代码:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值