首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用blueprint.copy时如何用sanic覆盖路由?

使用blueprint.copy时如何用sanic覆盖路由?
EN

Stack Overflow用户
提问于 2022-09-26 06:14:15
回答 1查看 26关注 0票数 0
代码语言:javascript
复制
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

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

EN

回答 1

Stack Overflow用户

发布于 2022-09-28 09:48:39

我从论坛上得到了答案。

代码语言:javascript
复制
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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73850187

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档