flask基礎之藍圖的使用(七)
阿新 • • 發佈:2018-12-07
前言
關於藍圖是什麼?或為什麼使用藍圖的詳細介紹,官方文件講的很詳細,不再贅述。簡單來說,在大型的應用中,我們不想檢視函式顯得雜亂無章,難以維護,將眾多的檢視函式按照Api的設計規則進行切割是一個好方法。
藍圖的簡單使用
- 第一步:建立藍圖
# testblue.py
from flask import Blueprint
testblue = Blueprint('blue', __name__)
@testblue.route('/index')
def index():
return 'OK'
- 第二步:註冊藍圖
# __init__.py from testblue import testblue app = Flask(__name__,template_folder='static/html') app.register_blueprint(testblue, url_prefix='/testblue') if __name__ == "__main__": app.run()
現在通過訪問http://127.0.0.1:5000/testblue/index就可以訪問到藍圖定義的api。
藍圖和應用的關係
藍圖的實現方式和應用十分相似,有著和app類似的執行機制,但它又不是一個應用,可以這樣說,app物件管理著多個藍圖,多個藍圖共享app的配置檔案,只有在app中註冊過的藍圖才會起作用,否則無效;app呼叫register_blueprint註冊藍圖,原理是:
# 原始碼 def register_blueprint(self, blueprint, **options): first_registration = False if blueprint.name in self.blueprints: ... else: self.blueprints[blueprint.name] = blueprint self._blueprint_order.append(blueprint) first_registration = True blueprint.register(self, options, first_registration) # 引數 blueprint:藍圖物件 url_prefix:url字首,即統一在該藍圖的所有的url前面新增一個字首;預設為空; subdomain:設定藍圖應該啟用的的子域,預設為空;
說明
首先,app通過blueprints字典收集管理所有的藍圖,_blueprint_order屬性列表儲存所有的藍圖物件;
藍圖物件blueprint呼叫register方法,會將藍圖下的所有檢視函式新增到app的view_functions屬性中,所有的Rule新增到APP的url_map屬性中;
Blueprint物件分析
- Blueprint的初始化
def __init__(self, name, import_name, static_folder=None, static_url_path=None, template_folder=None, url_prefix=None, subdomain=None, url_defaults=None, root_path=None): pass # 引數 name:設定藍圖的名字,這個名字是藍圖的標識,用來區分多個藍圖的不同; import_name:指定藍圖的資原始檔夾,也就是藍圖的位置,該引數是模組的名字 root_path:指定藍圖的資原始檔夾的絕對路徑,如果這個不是None,import_name的設定失效; static_folder:指定靜態檔案的路徑,相對路徑是相對於root_path; static_url_path:靜態檔案的url; template_folder:模板的路徑; url_prefix:url的字首,如果在藍圖註冊的時候也設定了該引數,那會使用註冊時的引數; subdomain:藍圖啟用的子域; url_defaults:預設的路徑引數和其對應的值的鍵值對,當其被設定後,本藍圖的所有檢視函式便擁有該引數
from flask import Blueprint
testblue = Blueprint('blue', __name__, url_defaults={'name':'cai'})
@testblue.route('/index')
def index(name):
print(name) # 擁有了預設的引數name
return 'OK'
- 重要的方法
Blueprint.add_url_rule:往app中註冊檢視函式和路由規則;
Blueprint.endpoint:裝飾器,直接往app的view_funcs新增檢視函式;
Blueprint.before_request:裝飾器,裝飾的方法本藍圖的每個請求呼叫檢視函式前執行
Blueprint.before_app_request:裝飾器,裝飾的方法app所有請求呼叫檢視函式前執行,但如果對應的藍圖有自己的before_request裝飾方法則執行自己的
Blueprint.before_app_first_request:裝飾器,裝飾的方法app所有請求第一次呼叫檢視函式前執行;
Blueprint.after_request:裝飾器,裝飾的方法本藍圖的每個請求呼叫檢視函式後執行,如果對應的app有全域性的after_request裝飾方法則執行全域性的
Blueprint.after_app_request:裝飾器,裝飾的方法app所有請求呼叫檢視函式後執行
Blueprint.teardown_request:裝飾器,裝飾的方法本藍圖的檢視函式是否有異常都會執行;
Blueprint.teardown_app_request:裝飾器,裝飾的方法app所有檢視函式是否有異常都會執行,但如果藍圖自己定義了則執行藍圖本身的
Blueprint.app_errorhandler:裝飾器,自定義app的所有的http請求的標準錯誤處理;
Blueprint.errorhandler:裝飾器,自定義本藍圖的所有http請求的標準錯誤處理
Blueprint.url_value_preprocessor:裝飾器,定義本藍圖所有的請求的url預處理
Blueprint.url_defaults:裝飾器,在app的url_default_functions中新增本藍圖的url生成的處理函式
Blueprint.app_url_value_preprocessor:裝飾器,定義app所有的請求的url預處理,如果藍圖有自己的預處理則使用藍圖本身的
Blueprint.app_url_defaults:裝飾器,在app的url_default_functions中新增app所有的url生成的處理函式,在呼叫url_for時會被呼叫
Blueprint.register_error_handler:方法,手動新增藍圖的標準錯誤處理
參考
- http://docs.jinkan.org/docs/flask/blueprints.html