整理基礎工程
- 請看第一天的配置檔案目錄,搭建了一個架構的基礎目錄
Application
settings
debug
作用
可以設定tornado是否工作在調試模式下面,預設為false,即工作在生産模式下
true的特性:
自動重新開機:
- tornado程式會監控源代碼檔案,會自動重新開機伺服器,減少我們手動重新開機的次數,提高開發效率
- 如果儲存後有錯誤,導緻重新開機失敗,修改好後,不會再重新開機了,需要我們手動進行重新啟動
- 在debug開啟後,那四個特性咱也不太會啊,咱就想着能夠重新開機就得了,那這可咋整,這個時候我們可以通過
設定,僅僅有第一個特性"autoreload" : True
取消緩存編譯的模闆:
- 單獨設定:
compiled_template_cache = False
- ,這個預設值為
,這裡要注意不是說我true
設定預設為啥,裡面就預設都是啥debug
- 你改完了模闆的内容,它得加載你改了的啊,不能還用緩存的内容,要不然你看不到修改的新結果,這可不行
- 雖然出于性能考慮,老也重新加載有點兒慢,但是沒事兒,畢竟開發中也不差這一點資源
取消緩存靜态檔案的HASH值
- 單獨設定:
static_hash_cache = False
- css檔案每次後面都有一個哈希值,這個哈希值能緩存
- 這樣我們都能重新加載這個css就OK了
提供追蹤資訊
- 如果我們的IndexHandler裡面抛出了一個異常,但是他自己沒有捕獲這個異常,就會生成一個追蹤的頁面
- 單獨設定:
serve_traceback = True
template_path:
設定模闆檔案目錄
static_path :
設定靜态檔案目錄
auto_escape :
當為None時,關閉項目的自動轉義
cookie_secret:
配置安全cookie秘鑰
xsrf_cookie:
當為True,開啟XSRF保護
login_url
用于定義登入的路徑,預設找這裡
路由
-
(r"/", index.IndexHandler),
- 傳的參數在路由那嘎達的字典類型的資料
D:.
│ config.py
│ readme.md
│ server.py
│ application.py
│
├─.idea
│ │ .gitignore
│ │ itcast_tornado.iml
│ │ misc.xml
│ │ modules.xml
│ │ vcs.xml
│ │ workspace.xml
│ │
│ └─inspectionProfiles
│ profiles_settings.xml
│
├─static # 靜态資源檔案夾
├─templates # 模闆檔案
├─upfile # 上傳檔案
│
└─views # 視圖
│ index.py # 首頁視圖
└─ __init__.py
version1.0
建立一個
index.py
檔案在
views
包下面,内容如下
from tornado.web import RequestHandler
class IndexHandler(RequestHandler):
def get(self):
self.write("main page info tornado!")
在
server.py
檔案中修改如下
import tornado.web
import tornado.ioloop
import tornado.httpserver
import tornado.options
import config
from views.index import IndexHandler
if __name__ == '__main__':
app = tornado.web.Application(
[
(r"/", IndexHandler)
]
)
httpServer = tornado.httpserver.HTTPServer(app)
httpServer.bind(config.options["port"])
httpServer.start(1)
tornado.ioloop.IOLoop.current().start()
其中的
config.py
不用動
options = {
"port": 8080,
"list": ["good", "nice", "handsome"]
}
version2.0
建立一個
application.py
的檔案,内容如下
import tornado.web
from views.index import IndexHandler
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r"/", IndexHandler)
]
super(Application,self).__init__(handlers)
然後服務端這麼改
import tornado.ioloop
import tornado.httpserver
import tornado.options
import config
from application import Application
if __name__ == '__main__':
app = Application()
httpServer = tornado.httpserver.HTTPServer(app)
httpServer.bind(config.options["port"])
httpServer.start(1)
tornado.ioloop.IOLoop.current().start()
視圖都不用動
from tornado.web import RequestHandler
class IndexHandler(RequestHandler):
def get(self):
self.write("main page info tornado!")
因為tornado不是Django那種大而全的,而是小而精的
是以配置也不用怎麼動
# 參數
options = {
"port": 8080
}
# 配置
settings = {
# static_path = "/",
"debug" : True
}
成了這就
然後我們再配置一個路由
home
在application裡面直接加就OK
import tornado.web
from views import index
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r"/", index.IndexHandler),
(r"/home", index.HomeHandler),
]
super(Application,self).__init__(handlers)
然後在視圖中再建立一個對應的類
from tornado.web import RequestHandler
class IndexHandler(RequestHandler):
def get(self):
self.write("main page info tornado!")
class HomeHandler(RequestHandler):
def get(self):
self.write(" this is home page content!")
重新開機服務即可在浏覽器中通路
http://127.0.0.1:8080/home
看到結果
配置路徑
Django中的那個BASE_DIRS挺好用的,我們也想有一個,那我們也可以整
import os
BASE_DIR = os.path.dirname(__file__)
# 參數
options = {
"port": 8080
}
# 配置
settings = {
# 這寫key的名字可不是随便起的奧,是寫好的,
# 就像upfile就沒有,你寫了也白扯
'static_path' : os.path.join(BASE_DIR,"static"),
'template_path' : os.path.join(BASE_DIR,"templates"),
"debug" : True
}
路由參數的傳遞
傳遞的方式和Django差不多,但也有不同之處,這裡直接上代碼
路由裡面這樣發
import tornado.web
from views import index
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r"/", index.IndexHandler),
(r"/sunck", index.SunckHandler,{'name':"victor",'age':19}),
]
super(Application,self).__init__(handlers)
視圖裡面這樣接
from tornado.web import RequestHandler
class IndexHandler(RequestHandler):
def get(self):
self.write("main page info tornado!")
class SunckHandler(RequestHandler):
# 該方法會在HTTP方法之前調用
def initialize(self,age,name) -> None:
self.age = age
self.name = name
def get(self):
print(self.age)
print(self.name)
self.write("sunck page info tornado!")
這裡不能再get方法中直接加上參數接受
需要重寫initialize方法,來對成員屬性進行定義
相似文章
tornado學習筆記day01 tornado學習筆記day02 tornado學習筆記day03 tornado學習筆記day04 tornado學習筆記day05 tornado學習筆記day06 tornado學習筆記day07 tornado學習筆記day08