-templates -views -__init__.py -user.py -order.py -app.py
app.pypython
from views import app if __name__ == '__main__': app.run()
init.pydjango
from flask import Flask,request app = Flask(__name__) #不導入這個不行 from . import account from . import order from . import user
user.pyflask
from . import app @app.route('/user') def user(): return 'user'
order.pyapp
from . import app @app.route('/order') def order(): return 'order'
注意點:只有是包的時候才能from.而後import 相對路徑進行導入url
容易發生循環導入問題code
-flask_pro -flask_test -__init__.py -static -templates -views -order.py -user.py -manage.py
__init_.pyblog
from flask import Flask app=Flask(__name__) from flask_test.views import user from flask_test.views import order app.register_blueprint(user.us) #註冊藍圖app.register_blueprint(藍圖的對象) app.register_blueprint(order.ord)
manage.pyget
from flask_test import app if __name__ == '__main__': app.run(port=8008)
user.pyit
from flask import Blueprint us=Blueprint('user',__name__) #藍圖的對象的名稱=Blueprint('自定義藍圖名稱',__name__) @us.route('/login') def login(): return 'login'
order.py
from flask import Blueprint ord=Blueprint('order',__name__) @ord.route('/test') def test(): return 'order test'
1 xxx = Blueprint('account', __name__,url_prefix='/xxx')
url_prefix
:藍圖URL前綴,表示url的前綴,在該藍圖下全部url都加前綴
2 xxx = Blueprint('account', __name__,url_prefix='/xxx',template_folder='tpls')
template_folder
:給當前藍圖單獨使用templates,向上查找,當前找不到,會找總templates
3 藍圖的befort_request
,對當前藍圖有效
4 大型項目,能夠模擬出相似於django中app的概念