我正在使用 django-restframework,我使用 postman POST json 数据到我的项目但是我得到了像 tittle 这样的错误,我已经设置了 raw 和 application/json 这里是来自 postman 的代码。
POST /account/post/reply/ HTTP/1.1
Host: localhost:8000
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: a0c7bd93-631e-4c7a-8106-87f018eaf7da
{
"user": "michael",
"userid": "1",
"ihelpid": 6,
"tittle": "6",
"info": "6",
"label": "3",
"tel": "dxy970525",
"picture1": null,
"picture2": null
}
我的代码真的很简单,就像:
from rest_framework.parsers import JSONParser,ParseError
class ReplyViewSet(viewsets.ModelViewSet):
"""
This viewset automatically provides `list` and `detail` actions.
"""
pagination_class=PageNumberPagination
queryset = Forum_reply.objects.all()
serializer_class = ReplySerializer
#filter
filter_backends = (DjangoFilterBackend, )
filter_fields = ['postID',]
def create(self, request, *args, **kwargs):
print(request.data)
data = JSONParser().parse(request)
return HttpResponse("ok")
使用视图集后,出现此错误,我已将其打印在外壳上,但没问题
原文由 Michael Porter 发布,翻译遵循 CC BY-SA 4.0 许可协议
我实际上遇到了同样的错误,如果您使用的是 ajax,则需要像下面这样使用 JSON.stringify(data):
@SeriForte 的回答为我指明了正确的方向