Tornadao—模板路径+渲染

  • 路径


    使⽤模板,需要仿照静态⽂件路径设置⼀样,向web.Application类的构造函数传
    递⼀个名为template_path的参数来告诉Tornado从⽂件系统的⼀个特定位置提
    供模板⽂件,如:
    
    
    app = tornado.web.Application(
     [(r'/', IndexHandler)],
     static_path=os.path.join(os.path.dirname(__file__), "statics"),
     template_path=os.path.join(os.path.dirname(__file__),
    "templates"),
    )

     

  • 渲染


    ├── statics
    │ ├── css
    │ │ ├── index.css
    │ │ ├── main.css
    │ │ └── reset.css
    │ ├── html
    │ │ └── index.html
    │ ├── images
    │ │ ├── home01.jpg
    │ │ ├── home02.jpg
    │ │ ├── home03.jpg
    │ │ └── landlord01.jpg
    │ ├── js
    │ │ ├── index.js
    │ │ └── jquery.min.js
    │ └── plugins
    │ ├── bootstrap
    │ │ └─...
    │ └── font-awesome
    │ └─...
    ├── templates
    │ └── index.html
    └── test.py

    在handler中使⽤render()⽅法来渲染模板并返回给客户端
    
    class IndexHandler(RequestHandler):
     def get(self):
     self.render("index.html") # 渲染主⻚模板,并返回给客户端。
    current_path = os.path.dirname(__file__)
    app = tornado.web.Application(
     [
     (r'^/$', IndexHandler),
     (r'^/view/(.*)$', StaticFileHandler, {"path":os.path.join(current_path, "statics/html")}),
     ],
     static_path=os.path.join(current_path, "statics"),
     template_path=os.path.join(os.path.dirname(__file__),
    "templates"),
    )

     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值