‘‘‘
蓝图
层次结构化路由,将应用程序逻辑划分为多个组或职责区域,类似于django多个app的url.py和setting.py的url.py
例如:不同模块负责不同功能,路由前缀不同
/api/t/?name="aw"
/api/s/?name="sw"
注册蓝图
t=Blueprint(‘t‘,url_prefix=‘/t‘)
api=Blueprint.group(s,t,url_prefix=‘/api‘)
app=Sanic(__name__)
app.blueprint(api)
‘‘‘
from sanic import Sanic,Blueprint
from sanic.response import json,file
from test import s
t=Blueprint(‘t‘,url_prefix=‘/t‘)
@t.post(‘/‘)
async def test(request):
name=request.form.get("name")
return json({‘name‘:name})
if __name__ == ‘__main__‘:
api=Blueprint.group(s,t,url_prefix=‘/api‘)
app=Sanic(__name__)
app.blueprint(api)
app.run(host="0.0.0.0",port=8000,debug=True)
原文:https://www.cnblogs.com/HHMLXL/p/14888431.html