当前位置: 首页 > 工具软件 > Blueprint > 使用案例 >

Blueprint使用==1

燕琨
2023-12-01

1 创建Blueprint

#user蓝本名字
#__name__蓝本所在的模块或包
bp = Blueprint('user',__name__)

2 注册Blueprint

from path import bp
#bp为实例化的Blueprint
#url_prefix为路由前缀
app.register_blueprint(bp, url_prefix='/api')

3 创建route

@bp.route('/test')
def test():
	return 'success'

4 访问route

localhost:8080/api/test

5 完整结构

app.py

from flask import Flask,Blueprint
from test import bp
app = Flask(__name__)

app.register_blueprint(bp, url_prefix='/api')
if __name__ == "__main__":
	app.run(host='0.0.0.0',port=8080,debug=True)

test.py

from flask import Blueprint
bp = Blueprint('test', __name__)
@bp.route('/test')
def test():
	return 'Test Success!'

run

#运行
python/python3 app.py
#访问
localhost:8080/api/test
#结果
Test Success!

 类似资料: