13

How can I have the django message framework work with the rest_framework?

Here is my view

@api_view(['GET', 'POST'])
def myview(request):
    if request.method == 'GET':
        #return a Response object
    else:
        #process post data
        messages.success(request, 'Success')
        return Response(response)

I encounter the following error

add_message() argument must be an HttpRequest object, not 'Request'

which is because the rest_framework does not use the normal HttpRequest object, used in django by default.

How can I use messaging framework with rest framework?

1
  • The problem seems to no longer happen in DRF 3.1 Commented Nov 10, 2015 at 11:12

1 Answer 1

30

DRF views do not use HttpRequest but use rest_framework.request.Request, (read here) you can access to the object that you need using

 messages.success(request._request, 'Success')

anyway this code have sense only if you are using BrowsableAPIRenderer

Sign up to request clarification or add additional context in comments.

4 Comments

i use a different http request to read the messages, hence this works great for me. thanks
We don't need to do this anymore (3.6). For me passing the request works just fine.
With Django 2.0, DRF 3.7.7, I cannot pass the request around any more.
It throws You cannot access body after reading from request's data stream

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.