Django Rest Framework:request.data的问题

时间:2014-12-05 04:18:10

标签: python django django-rest-framework

我正在尝试将Django rest框架用于我的服务器实现。我尝试POST时出现以下错误。

'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 个答案:

答案 0 :(得分:3)

尝试request.DATA而不是request.data

答案 1 :(得分:3)

版本3 (最新) - request.DATA 已替换为 request.dat

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']          
            )