zoukankan      html  css  js  c++  java
  • 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>
     
     
  • 相关阅读:
    windows操作系统的电脑越用越卡?简说几种原因和解决方法。
    不懂电脑也能自己重装系统,可视化傻瓜式一键重装系统不求人!!!
    023.Ubuntu常见个性化配置
    Blazor带我重玩前端(六)
    Blazor带我重玩前端(五)
    WSL2 配置及ubuntu18.04安装mysql8.0+
    用ThreadLocal来优化下代码吧
    基于canal的client-adapter数据同步必读指南
    「从零单排canal 07」 parser模块源码解析
    实用向—总结一些唯一ID生成方式
  • 原文地址:https://www.cnblogs.com/ssyfj/p/8513688.html
Copyright © 2011-2022 走看看