master
分支。步骤1 。 当数据发布到 apiview 时,打印 POST 查询字典。
@api_view(['GET', 'POST'])
def 片段列表(请求):
如果 request.method == 'POST':
从 pprint 导入 pprint 作为 pp
pp(request.data)
pp(请求.POST)
序列化器= SnippetSerializer(数据=request.data)
如果 serializer.is_valid():
序列化器.save()
返回响应(serializer.data,状态=状态。HTTP_201_CREATED)
返回响应(serializer.errors,状态=状态。HTTP_400_BAD_REQUEST)
第2步 。 将 '{"code": "print 123"}' 从可浏览 api 发布到snippet_list
apiview。
两个 pprint 的结果: {u'code': u'print 12dgdf3'}
<QueryDict: {}>
两个 pprint 的结果: {u'code': u'print 12dgdf3'}
{u'code': u'print 12dgdf3'}
mybad .. request.POST 中只有表单数据可用,我假设使用 Http POST 发送的任何数据都将在 request.POST 中。 关闭这个问题,tnx
mybad .. request.POST 中只有表单数据可用
没错,是的。 使用request.data
。
如果有人有兴趣在哪里可以找到官方答案:
https://www.django-rest-framework.org/tutorial/2-requests-and-responses/#request -objects
我还是不明白为什么POST
是空的。 :(
有教程或链接吗?
最有用的评论
没错,是的。 使用
request.data
。