flask使用Blueprint
阿新 • • 發佈:2021-02-07
問題
一定要把專案路徑匯入到系統路徑中
結構目錄
api_blueprint.py
from flask import Flask
import sys
import pathlib
# 把當前目錄加入系統路徑
base_dir = pathlib.Path(sys.argv[0]).parent.parent
if sys.path.count(base_dir) == 0 :
sys.path.append(str(base_dir))
#檢視系統路徑
# print(sys.path)
# 匯入api
from apis.dateil import detail
from apis.indexs import index
app = Flask(__name__)
# 使用藍圖訪問api
app.register_blueprint(detail)
app.register_blueprint(index)
if __name__ == '__main__':
app.run(host='0.0.0.0',port=5000)
index.py
# 匯入藍圖
from flask import Blueprint,request
# 使用藍圖
index = Blueprint('index',__name__)
@index.route( '/',methods=['GET'])
def index_view():
return '首頁'
detail .py
from flask import Blueprint,request
detail = Blueprint('detail',__name__)
@detail.route('/detei',methods=['GET','POST'])
def detail_view():
data = request.form.get('123')
return data