第二百六十三節,Tornado框架-基於正則的動態路由映射


Tornado框架-基於正則的動態路由映射

1、在路由映射條件里用正則匹配訪問路徑后綴
2、給每一個正則匹配規則(?P<設置名稱>)設置一個名稱,
3、在邏輯處理的get()方法或post()方法,接收這個正則名稱,就會接收到用戶訪問的后綴路徑

路由映射

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

import tornado.ioloop
import tornado.web                              #導入tornado模塊下的web文件
from controllers import index


settings = {                                    #html文件歸類配置,設置一個字典
    "template_path":"views",                    #鍵為template_path固定的,值為要存放HTML的文件夾名稱
    "static_path":"statics",                    #鍵為static_path固定的,值為要存放js和css的文件夾名稱
}


#路由映射
application = tornado.web.Application([         #創建一個變量等於tornado.web下的Application方法
    (r"/index/(?P<num>\d*)/(?P<nid>\d*)", index.indexHandler),                   #判斷用戶請求路徑后綴是否匹配字符串index,如果匹配執行MainHandler方法
],**settings)                                   #將html文件歸類配置字典,寫在路由映射的第二個參數里

if __name__ == "__main__":
    #內部socket運行起來
    application.listen(8888)                    #設置端口
    tornado.ioloop.IOLoop.instance().start()

邏輯處理

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

import tornado.ioloop
import tornado.web                              #導入tornado模塊下的web文件

#邏輯處理

class indexHandler(tornado.web.RequestHandler):  #定義一個類,繼承tornado.web下的RequestHandler類
    def get(self,num,nid):                                              #get()方法,接收get方式請求
        print(num,nid)
        self.render("index.html")                               #顯示index.html文件

 


免責聲明!

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



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