使用tornado实现简单的模板渲染程序

目录结构

py文件中的内容

settings.py

# -*- coding:utf-8 -*-
DATABASE = {
    'name':'whw',
    'pwd':'123'
}

app.py

# -*- coding:utf-8 -*-
# -*- coding:utf-8 -*-
import os

import tornado.web
import tornado.ioloop

import settings


SETTINGS = {
    "debug":True,
    "template_path":os.path.join(os.path.dirname(__file__),"templates"),
    "static_url_prefix":"/static/",
}


class MainHandler(tornado.web.RequestHandler):

    def get(self):
        self.render("index.html")


class UserHandler(tornado.web.RequestHandler):

    def get(self):
        self.render("user.html",msg=settings.DATABASE)


def make_app():
    return tornado.web.Application(

        [
            (r"/index", MainHandler),
            (r"/user", UserHandler),
        ],
        # 配置
        **SETTINGS
    )


if __name__ == '__main__':
    print(os.path.dirname(__file__))

    app = make_app()
    app.listen(9898)
    tornado.ioloop.IOLoop.current().start()

模板文件

user.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>User</title>
</head>
<body>
<h2 style="color:blue">用户信息:<a style="color: red">{{ msg }}</a></h2>
</body>
</html>

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Index</title>
</head>
<body>
<h1 style="color:red;">Hello Tornado</h1>
</body>
</html>

~~~

posted on 2019-10-27 22:58  江湖乄夜雨  阅读(579)  评论(0编辑  收藏  举报