day34-WEB框架

       

MVC               MTV

models    数据相关操作    models

views      模板HTML文件    templates 

controls     业务逻辑      controls

web框架代码 (2017-7-6 19:30:54)

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

import tornado.web
import tornado.ioloop
INPUT_LIST=[]
class MainHandler(tornado.web.RequestHandler):
    def get(self, *args, **kwargs):
        self.render('t1.html',ip=INPUT_LIST)
    def post(self, *args, **kwargs):
        name=self.get_argument('gg')
        print(name)
        INPUT_LIST.append(name)
        self.render('t1.html',ip=INPUT_LIST)
settings={
    'template_path':'template',
    'static_path': 'static',
}
app=tornado.web.Application([
    (r'/index',MainHandler),
],**settings)

if __name__=='__main__':
    app.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

    # import tornado.ioloop
    # import tornado.web
    # class MainHandler(torn
    #
    # ado.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()
View Code

html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{
            margin:0;
            padding:0;
        }
        .u{
            position: absolute;
            /*display: inline-block;*/
            list-style: none;
            background-color: rosybrown;
        }
        .l{
            float: left;
            display: inline-block;
            /*position: absolute;*/
        }
        .l2{

        }
        .l1{

        }
    </style>
    <link rel="stylesheet" href="static/common.css">
</head>
<body>
    <h1>GO</h1>
    <form method="post" action="/index">
        <div class="w">11111

        <ul class="u">

            <li>1</li>

        </ul>
        <input type="text" class="l3" name="gg">
        <input type="submit" value="subbb" class="l4">
        </div>
    </form>
    <h2>Show:</h2>
        <ul>
            {%for item in ip %}
            <li>{{item}}</li>
            {% end %}
        </ul>
</body>
</html>
View Code

 GET POST的区别是get可以在url里面传值 post在页面内传值

uimethod 直接写定义函数就好

uimodule要导入类

uimodule

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

class Go(UIModule):
    def render(self, *args, **kwargs):
        return 'gogoGo'
View Code

t1

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

import tornado.web
import tornado.ioloop
import uimodule as md
INPUT_LIST=[]
class MainHandler(tornado.web.RequestHandler):
    def get(self, *args, **kwargs):
        self.render('t1.html',ip=INPUT_LIST)
    def post(self, *args, **kwargs):
        name=self.get_argument('gg')
        print(name)
        INPUT_LIST.append(name)
        self.render('t1.html',ip=INPUT_LIST)

settings={
    'template_path':'template',
    'static_path': 'static',
    'ui_modules':md,
    # 'ui_method':mt,
}

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

if __name__=='__main__':
    app.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

    # import tornado.ioloop
    # import tornado.web
    # class MainHandler(torn
    #
    # ado.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()
View Code

html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{
            margin:0;
            padding:0;
        }
        .u{
            position: absolute;
            /*display: inline-block;*/
            list-style: none;
            background-color: rosybrown;
        }
        .l{
            float: left;
            display: inline-block;
            /*position: absolute;*/
        }
        .l2{

        }
        .l1{

        }
    </style>
    <link rel="stylesheet" href="static/common.css">
</head>
<body>
    <h1>GO</h1>
    <form method="post" action="/index">
        <div class="w">11111

        <ul class="u">

            <li>1</li>

        </ul>
        <input type="text" class="l3" name="gg">
        <input type="submit" value="subbb" class="l4">
        </div>
    </form>
    <h2>Show:</h2>
        <ul>
            {%for item in ip %}
            <li>{{item}}</li>
            {% end %}
        </ul>
        <h3>{% module Go()%}</h3>
</body>
</html>
View Code

cookies(2017-7-7 10:16:40)

#! bin/usr/evn python
# -*- coding:utf-8 -*-
import tornado.ioloop
import tornado.web

STU=0
class Indexhandler(tornado.web.RequestHandler):
    def get(self,*arg,**kwarg):
        self.render('index.html')

class Managerhandler(tornado.web.RequestHandler):
    def get(self,*arg,**kwarg):
        cooki=self.get_cookie('auth')
        if cooki=='1':
            self.render('manager.html')
        else:
            self.redirect('/login')

class Loginhandler(tornado.web.RequestHandler):
    def get(self,*arg,**kwarg):

        self.render('login.html',stu=STU)

    def post(self, *args, **kwargs):
        username=self.get_argument('username',None)
        pwd=self.get_argument('password',None)
        if username=='jiji' and pwd=='ge':
            self.set_cookie('auth','1')
            STU=1
        else:
            STU=0
        self.render('login.html',stu=STU)


class Logouthandler(tornado.web.RequestHandler):
    def get(self,*arg,**kwarg):
        self.set_cookie('auth','0')
        self.redirect('/login')

settings={
    # 'template_path':'views' #模板路径配置
}

app=tornado.web.Application({
    (r'/index',Indexhandler),
    (r'/login',Loginhandler),
    (r'/logout',Logouthandler),
    (r'/manager',Managerhandler),
},**settings)

if __name__=='__main__':
    app.listen(888)
    tornado.ioloop.IOLoop.instance().start()
View Code

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<style>
    h1{
        color: red;
        background-color: #9acfea;
    }
</style>
<body>
<h1>hellow word</h1>
<a href="/manager"><button>manager</button></a>
<a href="/login"><button>login</button></a>
</body>
</html>
View Code

manager.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>im manager</h1>
    <a href="/logout"><input type="button" value="logout"></a>
</body>
</html>
View Code

login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
        <form action="/login" method="post">
            <input type="text" name="username">
            <input type="password" name="password">
            <input type="submit" value="login">


            {% if stu==1 %}
                <span style="background-color: orangered; display:inline-block; ">has logined!!!</span>
            {% end %}
        </form>
        <a href="/manager"><button>manager</button></a>
        <a href="/index"><button>indxe</button></a>
        <a href="/logout"><button>logout</button></a>



</body>
</html>
View Code

  

原文地址:https://www.cnblogs.com/ezway/p/7120298.html