我正在尝试将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)
答案 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']
)