使用blueprint.copy时,如何在sanic中覆盖路由


from sanic import Blueprint
from sanic.response import json
from sanic import Sanic
app = Sanic('test')
bpv1 = Blueprint('bpv1', version=1)
@bpv1.route('/hello')
async def root(request):
return json('hello v1')
app.blueprint(bpv1)
bpv2 = bpv1.copy('bpv2', version=2)
@bpv2.route('/hello')
async def root(request):
return json('hello v2')
app.blueprint(bpv2)

当路由的实现属于不同的蓝图时,我想部分覆盖它们,但这会引发sanic_routing.exceptions.RouteExists

我怎样才能得到这个目标?

我从论坛上得到了答案。

bpv2 = bpv1.copy("bpv2", version=2)
bpv2._future_routes = {
route for route in bpv2._future_routes if route.uri != "/hello"
}

@bpv2.route("/hello")
async def root2(request):
return json("hello v2")

链接

https://community.sanicframework.org/t/how-to-overwrite-a-route-when-using-blueprint-copy/1067

最新更新