gpt4 book ai didi

python - 无法让 ErrorResponse 在 Django Rest Framework 上工作

转载 作者:太空宇宙 更新时间:2023-11-04 06:35:03 24 4
gpt4 key购买 nike

这个有效:

class Right(ResponseMixin, View):
"""Right"""
renderers = (JSONRenderer, XMLRenderer)

def get(self, request, right_id):
if not right_id:
return self.render(
Response(status.HTTP_400_BAD_REQUEST,
{"successful": False})
)

这不是:

class Right(ResponseMixin, View):
"""Right"""
renderers = (JSONRenderer, XMLRenderer)

# @default_json
def get(self, request, right_id):
if not right_id:
raise ErrorResponse(status.HTTP_400_BAD_REQUEST,
{"successful": False})

我收到一条错误消息:

错误响应在/v1/right/没有提供异常

或者:

Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/v1/right/

Django Version: 1.4.1
Python Version: 2.7.2
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles')
Installed Middleware:
('api.disable.DisableCSRF',
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/Users/agoodattitude/Envs/locker-proxy/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/Users/agoodattitude/Envs/locker-proxy/lib/python2.7/site-packages/django/views/generic/base.py" in view
48. return self.dispatch(request, *args, **kwargs)
File "/Users/agoodattitude/Envs/locker-proxy/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
69. return handler(request, *args, **kwargs)
File "/Users/agoodattitude/saff-py/locker-proxy/locker_proxy/api/resources.py" in get
125. error_message="right_id was not passed."))

Exception Type: ErrorResponse at /v1/right/
Exception Value:

有人知道“没有提供异常”是什么意思吗?

(请注意,我稍微更改了代码以降低复杂性,但问题依然存在......)

最佳答案

使用 http://django-rest-framework.org/api-guide/exceptions.html ,我将创建一个自定义异常,它将继承自 APIException 并引发该异常。这至少对我有用!

我知道我指的是新 2.x 版本的文档,而你指的是 0.4.x 版本,但它允许其他查看者关注主题和文档,并且 APIException 类也发生了变化与以前的版本相比只有一点点。

关于python - 无法让 ErrorResponse 在 Django Rest Framework 上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12425953/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com