zoukankan      html  css  js  c++  java
  • Tornado学习(五)

    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>
    08        {% 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了。

  • 相关阅读:
    Mybatis批量插入
    easyui中datagrid常见功能
    mysql下载和安装方式
    Mybatis注意事项
    ol3对地图上某些特定的经纬度进行标注
    ol3开发离线地图
    java利用poi生成excel文件后下载本地
    log4j的基本使用方法
    tomcat8.5之后版本,远程无法登录管理页面
    但构造函数返回对象时
  • 原文地址:https://www.cnblogs.com/wangzhilong/p/12549660.html
Copyright © 2011-2022 走看看