我只想尝试简单的Django休息框架代码,我使用APIVIEW并通过浏览器发布数据。
CODE:
class GetData(APIView):
def post(self, request, format=None):
if request.method == 'POST':
item_id = request.data.get('item_id')
return Response({"return": "OK ","item_id":item_id})
else:
return Response({"return": "NOT OK "})
上面的代码应该回显item_id,无论我" POST" ed。但结果总是为空。
POST数据:
{ "item_id" : "3456787"}
结果:
{"item_id":null,"return" :"OK "}
我不明白我哪里出错了。上面的代码在我的Loaclhost中运行正常但在生产服务器中不起作用。
答案 0 :(得分:0)
开始使用最新的django和DRF的新项目,并更改了以下内容
APP1 / views.py
class GetData(APIView):
def post(self, request, format=None):
if request.method == 'POST':
item_id = request.data.get('item_id')
return Response({"return": "OK ","item_id": item_id})
else:
return Response({"return": "NOT OK "})
仅在proj / settings.py中更改
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework'
]
应用程序/ urls.py
from django.conf.urls import url
from django.contrib import admin
from app1.views import GetData
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^test/', GetData.as_view())
]
查询:
curl -X Post http://localhost:8000/test/ -H "Content-Type: application/json" -d '{ "item_id":"3456787"}'
结果:
{"item_id":"3456787","return":"OK "}%