flask基礎之藍圖的使用(七)


前言

關於藍圖是什么?或為什么使用藍圖的詳細介紹,官方文檔講的很詳細,不再贅述。簡單來說,在大型的應用中,我們不想視圖函數顯得雜亂無章,難以維護,將眾多的視圖函數按照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:方法,手動添加藍圖的標准錯誤處理

參考


免責聲明!

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



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