【问题标题】:Passing multiple arguments in return 'Response' (python)传递多个参数以返回“响应”(python)
【发布时间】:2020-01-30 10:11:00
【问题描述】:

我在 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


    【解决方案1】:

    您可以返回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)
    

    【讨论】:

    • 感谢哈伦的回复
    【解决方案2】:

    添加字典

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

    希望对你有帮助

    【讨论】:

      【解决方案3】:

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

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

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

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

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

      【讨论】:

        猜你喜欢
        • 2013-10-09
        • 1970-01-01
        • 1970-01-01
        • 2010-10-16
        • 1970-01-01
        • 2021-09-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多