flask - 如何在Flask中请求最后一段url
问题描述
我觉得答案很简单,但我似乎无法弄清楚。我有一个网址:
我试图在我的代码中只使用“5”,因为它是 SQL 表中当前战斗的 ID。到目前为止,我已经尝试过
fight = Fight.query.filter_by(id=request.path).first()
然而,返回:
fight_card/fight/5
有什么方法可以使用“请求”来仅针对 5 个?先感谢您!
解决方案
您应该在路由定义中包含一个变量 , current_fight_id
。然后,您可以在视图函数中访问该变量。
@app.route('/fight_card/fight/<current_fight_id>')
def fight_route(current_fight_id):
print(current_fight_id) # use variable in route
或者,您可以使用您正在使用的方法,但修改返回的字符串。如果你有一个字符串:
endpoint = "fight_card/fight/5" # returned by your current code
您可以通过以下方式访问五个(current_fight_id):
current_fight_id = endpoint.split("/")[-1] # grab the segment after the last "/"
推荐阅读
- haskell - 使用 Haskell 的 Control.Monad.Random.Class.fromList 和 System.Random
- node.js - CORS 预检错误,Angular 到 Nodejs 本地服务器
- python - 根据列值重新排列行
- sql - 带有 JOINING 表的 MAX 和 SUM 函数
- android - 如何使垂直条颤动,如图所示
- android - 从 Sharesheet 接收所选应用程序选择的联系人
- python - ImportError:libmpi.so.12:无法打开共享对象文件:没有这样的文件或目录
- c++ - 是否允许通过指向另一个类的实例的指针发出信号?
- php - 我可以直接从谷歌云存储读取文件而无需使用 php 下载到本地服务器吗?
- php - 传递给 Illuminate\Database\Grammar::parameterize() 的参数 1 必须是数组类型,给定字符串 - 尝试通过 eloquent 插入 db 数据时