-templates
-views
-__init__.py
-user.py
-order.py
-app.py
app.py
from views import app
if __name__ == '__main__':
app.run()
init.py
from flask import Flask,request
app = Flask(__name__)
#不导入这个不行
from . import account
from . import order
from . import user
user.py
from . import app
@app.route('/user')
def user():
return 'user'
order.py
from . import app
@app.route('/order')
def order():
return 'order'
注意点:只有是包的时候才能from.然后import 相对路径进行导入
容易发生循环导入问题
-flask_pro
-flask_test
-__init__.py
-static
-templates
-views
-order.py
-user.py
-manage.py
_init.py
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.py
from flask_test import app
if __name__ == '__main__':
app.run(port=8008)
user.py
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的概念