“sanic”python中的占位符

问题描述

我最近一直在使用 python 中的 Sanic 模块,我想知道是否有办法在 url 中定义占位符。这有点难以解释,但我会尽力而为。

我希望它像这样。网址:/account/api/public/account/PlaceholderHere/externalAuths 有人能帮我解决这个问题吗?

解决方法

您可以在路由装饰器中的占位符周围使用 <>

my_route = Blueprint("my_route")

@my_route.route("/account/api/public/account/<placeholder>/externalAuths",methods=["GET"])
async def get_my_route(request,placeholder):
  print(f"here is my placeholder: {placeholder}")

在文档中阅读 parameters