2014-12-05 115 views
2

我正在嘗試爲我的服務器實現使用Django rest框架。我嘗試POST時出現以下錯誤。Django Rest框架:與request.data問題

'WSGIRequest' object has no attribute 'data' 

下面是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

版本3(最新) - request.DATA已被替換request.dat a

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

在2版 - 這是request.DATA

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

嘗試request.DATA代替request.data

+1

這工作。謝謝!我猜Django rest框架教程是錯誤的。 – jjb 2014-12-05 04:42:44

+4

我認爲他們正在更新即將推出的3.0版本的文檔。他們在新版本中將request.DATA交換到request.data。你可能會在使用2.3.4或2.4.4的地方使用request.DATA。你可以在這裏查看更改:http://www.django-rest-framework.org/topics/3.0-announcement/#request-objects – argaen 2014-12-05 08:59:07