如何从django自定义中间件类返回rest_framework.response对象?

时间:2022-03-21 02:40:37

I am trying to write a middleware class that ensures that the user is logged in. But the problem is this middleware class will only be applicable to a small set of views and these views return a DRF's Response object rather then the HTTPResponse object and these views are also decorated using api_view.

我正在尝试编写一个确保用户登录的中间件类。但问题是这个中间件类只适用于一小组视图,这些视图返回DRF的Response对象而不是HTTPResponse对象和这些视图也使用api_view进行装饰。

So when I try to return a Response object from the middle ware class it raises this error.

因此,当我尝试从中间ware类返回一个Response对象时,它会引发此错误。

 assert renderer, ".accepted_renderer not set on Response"
AssertionError: .accepted_renderer not set on Response

I've searched a bit on SO and I guess that the error is somehow related to api_view decorator. But I am confused on how to solve this problem.

我在SO上搜索了一下,我猜错误与api_view装饰器有某种关系。但我对如何解决这个问题很困惑。

Any help is appreciated. :)

任何帮助表示赞赏。 :)

1 个解决方案

#1


14  

I've just recently hit this problem. This solution doesn't use the Django Rest Framework Response, but if your server just returns JSON this solution might work for you.

我刚刚遇到这个问题。此解决方案不使用Django Rest Framework响应,但如果您的服务器只返回JSON,则此解决方案可能适合您。

New in django 1.7 is the JSONResponse response type.

django 1.7中的新功能是JSONResponse响应类型。

https://docs.djangoproject.com/en/1.7/ref/request-response/#jsonresponse-objects

https://docs.djangoproject.com/en/1.7/ref/request-response/#jsonresponse-objects

In the middleware you can return these responses without having all the "No accepted renderers" and "Response has no attribute encode" errors.

在中间件中,您可以返回这些响应,而不必使用“没有接受的渲染器”和“响应没有属性编码”错误。

It's very similar format to the DRF Response

它与DRF响应的格式非常相似

The import is as follows: from django.http import JsonResponse

导入如下:从django.http导入JsonResponse

And how you use it:

以及如何使用它:

return JsonResponse({'error': 'Some error'}, status=401)

return JsonResponse({'error':'some error'},status = 401)

Hopefully this helps you out!

希望这会帮助你!

#1


14  

I've just recently hit this problem. This solution doesn't use the Django Rest Framework Response, but if your server just returns JSON this solution might work for you.

我刚刚遇到这个问题。此解决方案不使用Django Rest Framework响应,但如果您的服务器只返回JSON,则此解决方案可能适合您。

New in django 1.7 is the JSONResponse response type.

django 1.7中的新功能是JSONResponse响应类型。

https://docs.djangoproject.com/en/1.7/ref/request-response/#jsonresponse-objects

https://docs.djangoproject.com/en/1.7/ref/request-response/#jsonresponse-objects

In the middleware you can return these responses without having all the "No accepted renderers" and "Response has no attribute encode" errors.

在中间件中,您可以返回这些响应,而不必使用“没有接受的渲染器”和“响应没有属性编码”错误。

It's very similar format to the DRF Response

它与DRF响应的格式非常相似

The import is as follows: from django.http import JsonResponse

导入如下:从django.http导入JsonResponse

And how you use it:

以及如何使用它:

return JsonResponse({'error': 'Some error'}, status=401)

return JsonResponse({'error':'some error'},status = 401)

Hopefully this helps you out!

希望这会帮助你!