Python 在错误响应中携带额外信息
返回额外数据(如错误类型响应中的错误代码)的最佳方法是什么#方法1不携带额外信息,而方法2不起作用Python 在错误响应中携带额外信息,python,django,tastypie,Python,Django,Tastypie,返回额外数据(如错误类型响应中的错误代码)的最佳方法是什么#方法1不携带额外信息,而方法2不起作用 #method 1 ImmediateHttpResponse(HttpBadRequest('error')) #cant carry extra info #method 2 class ErrorInfo(object): msg_id = '' msg_code = '' msg_content = '' class Resource(Resource):
#method 1
ImmediateHttpResponse(HttpBadRequest('error')) #cant carry extra info
#method 2
class ErrorInfo(object):
msg_id = ''
msg_code = ''
msg_content = ''
class Resource(Resource):
....
def get_object_list(self, request):
if ...:
error_info = ErrorInfo()
error_info.msg_id = 'rt'
error_info.msg_code = 'rerer'
obj = {'objects': error_info}
return self.create_response(request, obj)
如果有帮助,您可以使用即时HttpResponse(HttpResquest([…],status=403))是的,可以工作。立即引发HttpResponse(HttpBadRequest([{'msg_id':'1','msg_code':'r4r'}],status=403])status=403表示“禁止”。对于错误请求,使用status=400。当您使用HttpBadRequest时,默认值为400。