我使用 Django rest 框架 CreateAPIView 创建一些数据,这里是 View 。
class CompanyCreateApiView(CreateAPIView):
def post(self, request, *args, **kwargs):
print 'request ==', request
for each in args:
print 'args ==', each
for each in kwargs:
print 'keargs ==', each
import json
data=json.dumps({'status':'success'})
return Response(data, status=status.HTTP_200_OK)
现在我使用 python 请求模块调用这个 View 集,并且没有看到在 *Args 或 **kwargs 参数中打印的发布数据
**
import requests
r = requests.post('http://127.0.0.1:8000/api/create-company-profile/',data={'key':'value'})
print r.status_code
print r.text
**
请您参考如下方法:
您不会在其中看到任何 POST 数据;相反,您会在 request.data 中看到它。如 django-rest-framework documentation 中所述:
request.data returns the parsed content of the request body. This is similar to the standard request.POST and request.FILES attributes except that:
- It includes all parsed content, including file and non-file inputs.
- It supports parsing the content of HTTP methods other than POST, meaning that you can access the content of PUT and PATCH requests.
- It supports REST framework's flexible request parsing, rather than just supporting form data. For example you can handle incoming JSON data in the same way that you handle incoming form data.
