2

这有效:

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/ 处的 ErrorResponse 未提供异常

或者:

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: 

有人知道“不提供例外”是什么意思吗?

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

4

1 回答 1

0

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

我知道我指的是新 2.x 版本的文档,而您指的是 0.4.x 版本,但它允许其他查看者关注该主题和文档,并且 APIException 类从那时起变化不大以前的版本。

于 2012-11-26T16:42:18.163 回答