为什么JSON bool数据作为字符串传递到Django后端?

2024-04-19 16:55:00 发布

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

JSON data与Pythonrequest.params的对接:

http://localhost:8000/api/physicalservertask/list_for_home_workpanel/?has_physicalserver=false

如您所见,我在url中添加了has_physicalserver参数,它应该是逻辑上的'true',逻辑上的'false',但是在我的Django API中,我得到了一个str。你知道吗

has_physicalserver_list = query_params.pop('has_physicalserver')
has_physicalserver = has_physicalserver_list[0] if (isinstance(has_physicalserver_list, list) and len(has_physicalserver_list) > 0) else ''

enter image description here


Tags: apijsonfalselocalhosthttphomefordata
1条回答
网友
1楼 · 发布于 2024-04-19 16:55:00

查询参数是字符串。您可以使用json解析它。你知道吗

import json
has_physicalserver_list = query_params.pop('has_physicalserver') # string true/false
has_physicalserver_list = json.loads(has_physicalserver_list) # True/False

相关问题 更多 >