藍(lán)圖的作用是可以把一個(gè)應(yīng)用拆分成多個(gè)文件,以便于大型應(yīng)用的編寫(xiě)。
實(shí)現(xiàn)一個(gè)簡(jiǎn)單的藍(lán)圖。
本例中,文件目錄是這樣組織的:
/main.py # 主函數(shù)寫(xiě)在這
/routes # 按功能拆分路由
/todo.py
/user.py
我們根據(jù)功能,把和 Todo 功能相關(guān)的路由函數(shù)寫(xiě)到 todo.py
中,把和 User 相關(guān)的路由函數(shù)寫(xiě)到 user.py
中。
編寫(xiě) todo.py
:
from flask import (
render_template,
request,
redirect,
url_for,
Blueprint,
)
# 創(chuàng)建一個(gè) 藍(lán)圖對(duì)象 并且路由定義在藍(lán)圖對(duì)象中
# 然后在 flask 主代碼中「注冊(cè)藍(lán)圖」來(lái)使用
# 第一個(gè)參數(shù)是藍(lán)圖的名字, 第二個(gè)參數(shù)是套路
todo_routes = Blueprint('todo', __name__)
@todo_routes.route('/')
def index():
return 'Here is Todo!'
編寫(xiě) user.py
:
from flask import (
render_template,
request,
redirect,
url_for,
Blueprint,
)
# 創(chuàng)建一個(gè) 藍(lán)圖對(duì)象 并且路由定義在藍(lán)圖對(duì)象中
# 然后在 flask 主代碼中「注冊(cè)藍(lán)圖」來(lái)使用
# 第一個(gè)參數(shù)是藍(lán)圖的名字, 第二個(gè)參數(shù)是套路
user_routes = Blueprint('user', __name__)
@user_routes.route('/')
def index():
return 'Here is User!'
編寫(xiě) main.py
:
from flask import Flask
# 引入各個(gè)路由的藍(lán)圖對(duì)象
from routes.todo import todo_routes
from routes.user import user_routes
app = Flask(__name__)
# 設(shè)置 secret_key 來(lái)使用 flask 自帶的 session
# 這個(gè)字符串隨便你設(shè)置什么內(nèi)容都可以
app.secret_key = 'random string'
# 注冊(cè)藍(lán)圖
# 有一個(gè) url_prefix 可以用來(lái)給藍(lán)圖中的每個(gè)路由加一個(gè)前綴
# 比如:碰到 "/todo" 開(kāi)頭的 URL,就會(huì)用 todo_routes 的函數(shù)來(lái)處理
app.register_blueprint(todo_routes, url_prefix='/todo')
app.register_blueprint(user_routes, url_prefix='/user')
# 運(yùn)行代碼
if __name__ == '__main__':
# debug 模式可以自動(dòng)加載你對(duì)代碼的變動(dòng), 所以不用重啟程序
# host 參數(shù)指定為 '0.0.0.0' 可以讓別的機(jī)器訪問(wèn)你的代碼
config = dict(
debug=True,
host='0.0.0.0',
port=3000,
)
app.run(**config)
這時(shí)候,打開(kāi) "http://127.0.0.1:3000/todo/",“http://127.0.0.1:3000/user/”,就能見(jiàn)到藍(lán)圖工作了。
可能我們會(huì)對(duì) Blueprint('user', __name__)
中第一個(gè)參數(shù)的作用有疑問(wèn),這類似于 Django URLs 中的 name
參數(shù)的作用。
上面例子中,我們可以用 url_for('todo.index')
或者 url_for('user.index')
來(lái)找到對(duì)應(yīng)方法的 URL。
下面我們用藍(lán)圖來(lái)實(shí)現(xiàn) Django 風(fēng)格的文件目錄。
/main.py # 主函數(shù)寫(xiě)在這
/todo_app
/__init__.py
/models.py
/views.py
/templates # 屬于 todo 的模板文件夾
/todo_index.html
/user_app
/__init__.py
/models.py
/views.py
/templates # 屬于 user 的模板文件夾
/user_index.html
編寫(xiě) todo_app/views.py
:
from flask import (
render_template,
request,
redirect,
url_for,
Blueprint,
)
# 創(chuàng)建一個(gè) 藍(lán)圖對(duì)象 并且路由定義在藍(lán)圖對(duì)象中
# 然后在 flask 主代碼中「注冊(cè)藍(lán)圖」來(lái)使用
# 第一個(gè)參數(shù)是藍(lán)圖的名字, 第二個(gè)參數(shù)是套路
todo_routes = Blueprint(
'todo',
__name__,
template_folder = 'templates', # 設(shè)定模板目錄
url_prefix='/todo' # 設(shè)定后綴 url
)
@todo_routes.route('/')
def index():
return render_template('todo_index.html')
編寫(xiě) user_app/views.py
:
from flask import (
render_template,
request,
redirect,
url_for,
Blueprint,
)
# 創(chuàng)建一個(gè) 藍(lán)圖對(duì)象 并且路由定義在藍(lán)圖對(duì)象中
# 然后在 flask 主代碼中「注冊(cè)藍(lán)圖」來(lái)使用
# 第一個(gè)參數(shù)是藍(lán)圖的名字, 第二個(gè)參數(shù)是套路
user_routes = Blueprint(
'user',
__name__,
template_folder = 'templates',
url_prefix='/user'
)
@user_routes.route('/')
def index():
return render_template('user_index.html')
編寫(xiě) main.py
:
from flask import Flask
# 引入各個(gè)路由的藍(lán)圖對(duì)象
from todo_app.views import todo_routes
from user_app.views import user_routes
app = Flask(__name__)
# 設(shè)置 secret_key 來(lái)使用 flask 自帶的 session
# 這個(gè)字符串隨便你設(shè)置什么內(nèi)容都可以
app.secret_key = 'random string'
# 注冊(cè)藍(lán)圖
app.register_blueprint(todo_routes)
app.register_blueprint(user_routes)
# 運(yùn)行代碼
if __name__ == '__main__':
config = dict(
debug=True,
host='0.0.0.0',
port=3000,
)
app.run(**config)