在返回'Response'(python)中传递多个参数

问题描述 投票:1回答:3

我正在Angular中工作,并且正在使用Http请求和响应。是否可以在“响应”中发送多个参数。

角度文件:

this.http.get("api/agent/applicationaware").subscribe((data:any)...

python文件:

def get(request):
    ...
    return Response(serializer.data)

我想在响应中发送多个参数。喜欢

return Response(serializer.data,obj.someothervalue)

您能帮我吗?在此先感谢:)

python angular httprequest httpresponse
3个回答
2
投票

您可以返回dictionary

return Response({'serializer_data': serializer.data, 'some_other_value': obj.someothervalue})

或者您可以将someothervalue附加到serializer.data

data = serializer.data
data['someothervalue'] = obj.someothervalue
return Response(data)

如果obj.someothervalue也是一个字典,那么您可以合并两个字典:

data = serializer.data.copy()
data.update(obj.someothervalue)
return Response(data)

0
投票

添加字典

dict = {}
dict['details'] = serializer.data
dict['other'] = someotherdata
return Response(dict)

希望这会有所帮助


0
投票

您需要先确定API响应的协议,然后才能进行相应的计划。

要回答您的问题,是的,您可以发送多个参数作为响应。

您可以创建字典/列表,然后将要发送的所有值放入其中。现在发送该词典/列表作为您的回复。我会推荐这样的东西:

response = dict()
response["data"] = dict()
response["data"]["serializer_data"] = serializer.data
response["data"]["some_other_value"] = obj.someothervalue

return Response(response)

在响应的接收端,您必须以与发送数据类似的方式读取响应数据。

© www.soinside.com 2019 - 2024. All rights reserved.