zoukankan      html  css  js  c++  java
  • Tornado框架中视图模板Template的使用

    上文的程序中有这样一段:

    class MessageHandler(tornado.web.RequestHandler):
        def get(self):
            self.write('''
    <html>
    <head>
            <title>Please Input Message</title>
    </head>
    <body>
            <form action="/message" method="post">
                    <input type="text" name="message"><br>
                    <input type="submit" value="submit">
            </form>
    </body>
    </html>''' 
            )
        def post(self):
            #self.set_header("Content-Type", "text/plain")
            self.write("You wrote <h1>" + self.get_argument("message") + "</h1>")

    当收到GET请求时,返回一段HTML表单。

    上面的这种写法,将html写在python代码中,灵活性差,而且view代码与controller代码混合在一块,不符合MVC的原则。

    所以我们采用Tornado中的模板。

    新建form.html:

    <html>
        <head>
        <title>{{title}}</title>
        </head>
        <body>
            <form action="/message" method="post">
                <input type="text" name="message" value="please input.">
                <input type="submit" value="submit">
            </form>
        </body>
    </html>

    然后将上面的python代码修改为:

    class MessageHandler(tornado.web.RequestHandler):
        def get(self):
            self.render("form.html", title="Input Message")
        def post(self):
            #self.set_header("Content-Type", "text/plain")
            self.write("You wrote <h1>" + self.get_argument("message") + "</h1>")

    这样代码简洁了很多。

    完整的代码是:

    import tornado.ioloop
    import tornado.web
    
    class MainHandler(tornado.web.RequestHandler):
        def get(self):
            self.write("<h1>This is Home Page!</h1>")
    
    class StoryHandler(tornado.web.RequestHandler):
        def get(self, story_id):
            self.write("You request the story <h1>" + story_id + "</h1>")
    
    class MessageHandler(tornado.web.RequestHandler):
        def get(self):
            self.render("form.html", title="Input Message")
        def post(self):
            #self.set_header("Content-Type", "text/plain")
            self.write("You wrote <h1>" + self.get_argument("message") + "</h1>")
    
    application = tornado.web.Application([
        (r"/", MainHandler),
        (r"/story/([0-9]+)", StoryHandler),
        (r"/message", MessageHandler),
        ])
    
    if __name__ == '__main__':
        application.listen(8888)
        tornado.ioloop.IOLoop.instance().start()
  • 相关阅读:
    C++ 获取图片文件信息
    java中redis的分布式锁工具类
    java中的redis工具类
    mysql中的sql查询优化
    利用Linux中的crontab实现分布式项目定时任务
    MYSQL的REPLACE和ON DUPLICATE KEY UPDATE使用
    redis学习三,Redis主从复制和哨兵模式
    redis学习五,redis集群搭建及添加主从节点
    String 转化成java.sql.Date和java.sql.Time
    SpringMVC配置双数据源,一个java项目同时连接两个数据库
  • 原文地址:https://www.cnblogs.com/inevermore/p/4190382.html
Copyright © 2011-2022 走看看