Python之tornado框架原理


Python web框架

1、簡單概念
tornado
  socket、邏輯處理
Django
flask
  邏輯處理
  第三方處理模塊(包含了socket)
  jinja2模塊

Models 數據庫處理
Views 模板HTML文件
Controllers 業務邏輯(根據訪問的URL不同,調用不同函數)

MVC --MVC框架


Models 數據庫處理
Templates 模板HTML文件
Views 業務邏輯(根據訪問的URL不同,調用不同函數)

MTV框架



 

Python框架原理圖

 2、tornado框架簡單介紹

s1的代碼是這樣的

import tornado.ioloop
import tornado.web



class MainHandler(tornado.web.RequestHandler):
    def get(self):
        # self.write("Hello, world")
        self.render('js2.html')

settings = {
    'template_path':'template',#路徑配置,就是自動取到該路徑下尋找文件
    'static_path':'static',#靜態文件配置,需要特殊處理
}



#路由映射,根據不同url對應到不同的類里面
application = tornado.web.Application([
    (r"/index", MainHandler),
],**settings)
#第二個接收配置文件

if __name__ == "__main__":
    application.listen(8888)#監聽端口
    tornado.ioloop.IOLoop.instance().start()

這樣簡單配置之后就可以運行一個簡單的網頁了,訪問頁面則可以返回js2.html的頁面了

總結起來是五點

2、利用tornado框架實現數據提交

主要是修改上面s1的代碼

import tornado.ioloop
import tornado.web

input_list=[]#用來接收用戶的數據

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        # self.write("Hello, world")
        self.render('js2.html',xxxooo = input_list)
    def post(self, *args, **kwargs):


        name = self.get_argument('jjj')#根據value提取用戶輸入的名字
        input_list.append(name)
        print(name)
        #1、打開js2.html文件,讀取內容(包含特殊語法)
        #2、xxxooo = [11,22,333,44] 讀取內容(包含特殊語法)
        #3、得到新的字符串
        #4、將得到新的字符串返回給用戶
        self.render('js2.html',xxxooo =  input_list)
settings = {
    'template_path':'template',#路徑配置,就是自動取到該路徑下尋找文件
    'static_path':'static',#靜態文件配置,需要特殊處理
    'static_url_prefix':'/sss/',#標記文件開始的名字
}


#路由映射,根據不同url對應到不同的類里面
application = tornado.web.Application([
    (r"/index", MainHandler),
],**settings)
#第二個接收配置文件

if __name__ == "__main__":
    application.listen(8888)#監聽端口
    tornado.ioloop.IOLoop.instance().start()

  下面是js2的html文件,寫了一個表單

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>你好</title>
    <link rel="stylesheet" href="/sss/commons.css">
</head>
<body>

<P style="font-size: 50px"> js3</P>
<h1>提交內容</h1>
<form method="post" action="/index">
    <input type="text" name="jjj">
    <input type="submit" value="提交">
</form>


<h1>顯示內容</h1>
<ul>
    {% for item in xxxooo %}
    <li>{{item}}</li>
    {% end %}
</ul>
<!--<script src="sss/jayzhou.js"></script>-->
</body>
</html>

  運行如圖,數據自動添加到下面

3、對於模板語言,主要有三類

 

模板語言分為三類:
{{}}表達式
{% if %}{% end %}
  例如:
  <ul>
    {% for item in xxxooo %}
    <li>{{item}}</li>
    {% end %}
  </ul> #模板語言里通過for循環展示數據
自定義:
  uimethod/uimodle

 

對於自定義的uimethod,我們先在tornado_demo里面簡單寫個uimethod模塊,如圖

然后在上面的s1里面配置好設置文件

settings = {
    'template_path':'template',#路徑配置,就是自動取到該路徑下尋找文件
    'static_path':'static',#靜態文件配置,需要特殊處理
    'static_url_prefix':'/sss/',#標記文件開始的名字
    'ui_methods':uimethod#這個是我們導入的文件
}

  在js2.html文件里面的模板語言

<ul>
    {% for item in xxxooo %}
    <li>{{item}}</li>
    <h2>{{func(item)}}</h2>
    {% end %}
<h2>{{func(amp)}}</h2>

  func函數是我們在uimethod里面自定義的函數,輸出如圖

對於uimodule,我們需要寫創建一個新的模塊,如圖,當然也要在settings里面設置好

js2.html文件里面要這樣寫

<ul>
    {% for item in xxxooo %}
    <li>{{item}}</li>

    {% end %}
<h2>{{func(amp)}}</h2>
<h3>{% module custom() %}</h3>
    <!--調用自定義的custom模塊-->

</ul>

  輸出如圖

 tarnodo自己也有一些模板語言

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM