簡體   English   中英

使用 blueprint.copy 時如何覆蓋 sanic 中的路由?

[英]how to overwrite a route in sanic when using blueprint.copy?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM