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"),
    )
发布了258 篇原创文章 · 获赞 6 · 访问量 3534

猜你喜欢

转载自blog.csdn.net/piduocheng0577/article/details/105060919