Tornado---路由、模板、UIMethod-UIModule

快速上手

import tornado.ioloop
import tornado.web


class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")


application = tornado.web.Application([
    (r"/index", MainHandler),
])

if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

第一步:执行脚本,监听 8888 端口
第二步:浏览器客户端访问 /index –> http://127.0.0.1:8888/index
第三步:服务器接受请求,并交由对应的类处理该请求
第四步:类接受到请求之后,根据请求方式(post / get / delete …)的不同调用并执行相应的方法
第五步:方法返回值的字符串内容发送浏览器

路由系统

import tornado.ioloop
import tornado.web


class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, worlder")


class StoryHandler(tornado.web.RequestHandler):
    def get(self, story_id):
        self.write("You requested the story " + story_id)


class BuyHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("buy.wupeiqi.com/index")


application = tornado.web.Application([
    (r"/index", MainHandler),
    (r"/story/([0-9]+)", StoryHandler),
])

application.add_handlers('iyongfei.com$', [
    (r'/index', BuyHandler),
])

if __name__ == "__main__":
    application.listen(80)
    tornado.ioloop.IOLoop.instance().start()

模板引擎

这里写图片描述

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.render("index.html", list_info=[11, 22, 33])

settings = {
    'template_path': 'templates',
    'static_path': 'static',
}

application = tornado.web.Application([
    (r"/index", MainHandler),
], **settings)

if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

页面前端:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>老男孩</title>
    <link href='{{static_url("common.css")}}' rel="stylesheet" />
</head>
<body>
    <div id="my_div">
        <ul>
            {% for item in list_info %}
                <li>{{item}}</li>
            {% end %}
        </ul>
    </div>
</body>
</html>

css文件

#my_div{
    background: green;
}

效果图如下:
这里写图片描述

模板继承

base.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
     {% block CSS %}{% end %}
</head>
<body>

 {% block RenderBody %}{% end %}
 {% block JavaScript %}{% end %}
</body>
</html>

index.html

{% extends 'base.html'%}

{% block CSS %}
     <link href='{{static_url("common.css")}}' rel="stylesheet" />
{% end %}

{% block RenderBody %}
    <div id="my_div">
        <ul>
            {% for item in list_info %}
                <li>{{item}}</li>
            {% end %}
        </ul>
    </div>

{% end %}

{% block JavaScript %}

{% end %}

自定义UIMethod以UIModule

这里写图片描述
定义ui_methods.py

def tab(self):
    return 'UIMethod'

ui_modules.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from tornado.web import UIModule
from tornado import escape

class custom(UIModule):

    def render(self, *args, **kwargs):
        return escape.xhtml_escape('<h1>%s</h1>'%args[0])

使用UIMethod以UIModule
s1.py

import tornado.ioloop
import tornado.web
import ui_modules as md
import ui_methods as mt


##############传入对象#############
class Calculation:
    def sum(self, a, b):
        return a + b


class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.render('indexx.html', fun=self.fun, cal=Calculation)

    #############传入类方法#############
    def fun(self):
        return "this is funcation"

settings = {
    'template_path': 'templates',
    'static_path': 'static',
    'static_url_prefix': '/static/',
    'ui_methods': mt,
    'ui_modules': md,
}

application = tornado.web.Application([
    (r"/indexx", MainHandler),
], **settings)

if __name__ == "__main__":
    application.listen(8009)
    tornado.ioloop.IOLoop.instance().start()

indexx.html

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    {% module custom(123) %}
    <br>
    {{ tab() }}
<br>
{{fun()}}
<br>
{{ cal().sum(6,9) }}
</body>

效果图:
这里写图片描述

猜你喜欢

转载自blog.csdn.net/u013210620/article/details/80368450