初学tornado的记录

1、tonado的模板文件的配置和前后端的交互代码如下:

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


import tornado.ioloop
import tornado.web

info = []


class MainHandler(tornado.web.RequestHandler):

    def get(self):
        self.render("html.html", inforrender = info)

    def post(self, *args, **kwargs):
        self.write("hello world!")
        name = self.get_argument("names")
        info.append(name)
        self.render('html.html', inforrender = info)


settings = {
    'template_path': 'template1',
    'static_path': 'static',
    'static_url_prefix': '/static/',
     }


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

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

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<from action="http://www.shouhu.com/web" method="post" enctype="multipart/form-data">
    <p>用户名<input type="text" name="username"></p>
    <p>密码<input type="password" name="pwd"></p>
    <p>提交<input type="submit" value="提交"></p>
    <p><input type="radio" name="sex" value="1"></p>
    <p><input type="radio" name="sex" value="2"></p>
    <p><input type="file" name="file_name(声明一下)"></p>
    <select name="language" multiple="multiple" size="2">
        <optgroup label="chengxu">
            <option value="1">java</option>
            <option value="2">javascript</option>
            <option value="3">python</option>
        </optgroup>
        <optgroup label="biancheng">
            <option value="1">java1</option>
            <option value="2">javascript2</option>
            <option value="3">python3</option>
        </optgroup>
    </select>
    <p>备注<textarea name="qq"></textarea></p>

</from>

<body>

<h1>My first title</h1>
        <p1>this is my first paragraph</p1>
        <a>This is a link</a>
<form method="post" action="/index" >
    <input type="text" name ="names">
    <input type = "submit" value="提交">
</form>
<h>content show</h>
    <lu>
        {%for item in inforrender %}
        <li>{{item}}</li>
        {%end%}
    </lu>
</body>
</html>
html.html

 

2、自定义UIMethod以UIModule:

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

class custom(UIModule):

    def render(self, args):
        
        return args
uimodules.py

 

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


def fun(self, arg):
    return arg
    return "hello"
uimethods.py

 

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


import tornado.ioloop
import tornado.web
import uimethods as mt
import uimodules as md
info = []


class MainHandler(tornado.web.RequestHandler):

    def get(self):
        self.render("html.html", arg="123", args="456", inforrender=info)

    def post(self, *args, **kwargs):
        self.write("hello world!")
        name = self.get_argument("names")
        info.append(name)
        self.render('html.html', inforrender=info)


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


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

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

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<from action="http://www.shouhu.com/web" method="post" enctype="multipart/form-data">
    <p>用户名<input type="text" name="username"></p>
    <p>密码<input type="password" name="pwd"></p>
    <p>提交<input type="submit" value="提交"></p>
    <p><input type="radio" name="sex" value="1"></p>
    <p><input type="radio" name="sex" value="2"></p>
    <p><input type="file" name="file_name(声明一下)"></p>
    <select name="language" multiple="multiple" size="2">
        <optgroup label="chengxu">
            <option value="1">java</option>
            <option value="2">javascript</option>
            <option value="3">python</option>
        </optgroup>
        <optgroup label="biancheng">
            <option value="1">java1</option>
            <option value="2">javascript2</option>
            <option value="3">python3</option>
        </optgroup>

    </select>
    <p>备注<textarea name="qq"></textarea></p>

</from>
<body>
<link rel = "stylesheet",href='{{static_url("commons.css")}}'>
<div class cy>haaaa</div>
<h1>My first title</h1>
        <p1>this is my first paragraph</p1>
        <a>This is a link</a>
<form method="post" action="/index" >
    <input type="text" name ="names">
    <input type = "submit" value="提交">

</form>
<h>content show</h>
    <lu>
        {% for item in inforrender %}
            {% if item == 'alex' %}
                <li style="color:red">{{item}}</li>
            {% else %}
                <li style="color:blue">{{item}}</li>
            {% end %}
        {% end %}
    </lu>
<h>hello</h>
{% module custom(args)%}
{{fun(arg)}}
</body>
</html>
html.html

注:1、2上述忽略css文件。

 

3、常用的模板内置函数:

escape: tornado.escape.xhtml_escape 的別名
xhtml_escape: tornado.escape.xhtml_escape 的別名
url_escape: tornado.escape.url_escape 的別名

handler: 当前的 RequestHandler 对象

request: handler.request 的別名

current_user: handler.current_user 的別名

static_url: for handler.static_url 的別名
xsrf_form_html: handler.xsrf_form_html 的別名
 

 

posted @ 2017-03-13 16:02  Jhon23  阅读(144)  评论(0编辑  收藏  举报