python---tornado初识(1)

# coding:utf8
# __author:  Administrator
# date:      2018/3/6 0006
# /usr/bin/env python

import tornado.ioloop
import tornado.web
import uimethod as mt
import uimodule as md

name_list=[]

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        # self.write("Hello World")
        # 默认当前路径寻找
        # print(self.get_argument('name'))
        # print(self.get_argument('age'))
        self.render("s1.html",ccc=name_list,nm="mmp")

    def post(self, *args, **kwargs):
        name=self.get_argument('xxx')
        name_list.append(name)
        self.render("s1.html",ccc=name_list)

st ={
    "template_path": "template",#模板路径配置
    "static_path":'commons',    #js css等静态文件路径配置    无论这里配置了什么路径,在静态文件中使用都是用static 可以使用static_url获取资源,会生成缓存(后缀v=..)
    #"static_url_prefix":'/ss/', #在static_path必须存在的基础上 类似于对其取了一个别名
    #若是没有static_url_prefix,则在静态文件中的资源获取为static/s1.css
    #当存在static_url_prefix时,(前提已经存在static_path),这时具体路径程序已经获取,你只需要在资源前面加上这个前缀,不需要自己去写具体url
    #就是可以看做为static_path起了一个别名
    #static_url_prefix了解即可,不常用
    'ui_methods':mt, #自定义函数在模板中使用{{}}
    'ui_modules':md, #自定义类在模板中使用{% %}
}

#路由映射   匹配执行,否则404
application = tornado.web.Application([
    ("/index",MainHandler),
],**st)

if __name__=="__main__":
    application.listen(8080)

    #io多路复用
    tornado.ioloop.IOLoop.instance().start()
uimethod.py
def func(self,nm):
    return '123'+nm
uimodule.py
from tornado.web import UIModule
from tornado import escape

class custom(UIModule):
    def render(self, *args, **kwargs):
        return "1234"

模板中使用

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!--<link rel="stylesheet" href="{{static_url('s1.css')}}"/>-->
    <link rel="stylesheet" href="static/s1.css">
</head>
<body>
    <h1>fagfagaggwag</h1>
    <form method="post" action="/index">
        <input type="text" name="xxx">
        <input type="submit" value="提交">
    </form>
    <ul>
        {% for item in  ccc %}
        <li>{{item}}</li>
        {% end %}
        <h2>{{ func(nm) }}</h2>
        <h3>{% module custom() %}</h3>
    </ul>
</body>
</html>

 

 
 
posted @ 2018-03-06 12:15  山上有风景  阅读(210)  评论(0编辑  收藏  举报