[Django]-Django rest framework custom Response middleware

9đź‘Ť

âś…

return Response(request) is not something django can handle. Ps you are passing request object to Response what does that mean?

Actually Request and Response classes from rest framework are not compatible with django. rest framework wraps WSGIRequest (django’s builtin) with Request object in APIView and after getting Response object from view, it creates HttpResponse object by unwrapping it. So Response object can only be used in rest framework views. You can use JsonResponse from django.http in middleware like this

from django.http import JsonResponse


if request.user.is_authenticated:
    if request.user.session_key != Session.objects.filter(session_key__in = request.user.session_key):
        logout(request)
        return JsonResponse({'logout': True}) # or whatever data you want to return 
return self.get_response(request)
👤Nafees Anwar

3đź‘Ť

First of all: if you are using something other than rest_framework.authentication.SessionAuthentication as an authentication_class, then request.user is set outside of middlewares (somewhere during View.dispatch)

If you are sure that request.user is always corresponding to request.user inside rest_framework views and just want to return a response:

A. Look at the APIView.finalize_response: it’s relatively “complex” because it can use different renderers (depending on accept-content request header), it may change headers. If you can access instance of your view in the middleware, then you can call view.finalize_response(request, response), if you don’t have it then you can try doing rest_framework.views.APIView().finalize_response(...)

B. You can use django.http.HttpResponse: manually generate string for body, and specify appropriate content_type. And django.http.JsonResponse may be handy too.

C. I assumed that instead of return Response(request) you really are doing something else (like Response(data=...)). If you just need to return results from view, then remove return Response(request), so that return self.get_response(request) kicks in. But you are making a logout, so maybe you should return some instance of django.http.HttpResponseRedirect

👤imposeren

Leave a comment