2

I am attempting to use Django rest framework for my server implementation. I get the following error when I attempt to POST.

'WSGIRequest' object has no attribute 'data'

Here is the code for the view.py

from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response
from whiteboards.models import Whiteboard, Path, Point
from whiteboards.serializers import WhiteboardSerializer

@api_view(['GET', 'POST'])
def whiteboard_list(request):
   """
   List all whiteboards, or create a new whiteboard.
   """


   if request.method == 'GET':
       print('GET')
       whiteboards = Whiteboard.objects.all()
       serializer = WhiteboardSerializer(whiteboards, many=True)
       return Response(serializer.data)

   elif request.method == 'POST':
       print('POST')
       d = request.data
       print('data broke')
       serializer = WhiteboardSerializer(data=d)
       print("created serializer")
       if serializer.is_valid():
           serializer.save()
           print("It's valid")
           return Response(serializer.data, status=status.HTTP_201_CREATED)
       return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

2 Answers 2

3

In version 3 (latest) - request.DATA has been replaced with request.data:

user = dict(
                    full_name=request.data['full_name'],
                    password=request.data['password'],
                    email=request.data['email']                
                )

In version 2 - it was request.DATA:

user = dict(
                full_name=request.DATA['full_name'],
                password=request.DATA['password'],
                email=request.DATA['email']          
            )
Sign up to request clarification or add additional context in comments.

Comments

3

try request.DATA instead of request.data

2 Comments

That worked. Thank you! I guess the Django rest framework tutorial was wrong.
I think they are updating the documentation for the new upcoming 3.0 version. They swapped request.DATA to request.data in the new version. You may be using 2.3.4 or 2.4.4 where request.DATA is the way to go. You can check the change here: django-rest-framework.org/topics/3.0-announcement/…

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.