如何在djago rest框架中使用“/”处理get请求参数?

2024-06-16 14:46:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Django Rest API框架的新手。我正在开发从数据库中提取数据的GETAPI。我在URL中传递参数,如下所示。如果“item_name”中有“/”,则此API失败

API URL:http://localhost:8000/api/getstat/**item_name**
Working: http://localhost:8000/api/getstat/**completed**
Fails for: http://localhost:8000/api/getstat/**NotComplete/failed**

Error: The current path, /api/getstat/**NotComplete/failed**, didn't match any of these.

关于如何处理此问题的任何信息/帮助都非常有用,或者如何逃避?因此,它不会被视为路径


Tags: djangoname框架restapi数据库localhosthttp
2条回答

您需要将斜杠转义为%2F

这是/的标准URL编码

正如Matt在评论中所建议的,在传递路径时尝试使用查询参数而不是参数,这对我很有效。Django支持以下两种方法,但其中一种方法在上述情况下失败

Failed: http://localhost:8000/api/getstat/**NotComplete/failed**
worked: http://localhost:8000/api/getstat/?**item=NotComplete/failed**

Read param like "reportID = self.request.query_params.get('item')" in view.

这对我有用

相关问题 更多 >