使用REST Framework在Django中从POST获取JSON数据 [英] Get JSON data from POST in Django with REST Framework
问题描述
我试图在Django中为Gspread的Google Spreadsheet构建REST API,我遇到了很多问题(因此,如果有人碰巧有一个完整的示例,请随时分享....?:)).这些问题之一是我试图接收POST请求的JSON(当然,稍后还会收到其他请求).但是,这是失败的.这是我的代码:
I am attempting to build a REST API in Django for a Google Spreadsheet with Gspread, I'm running into a lot of issues (so if anyone happens to have a full example lying around, feel free to share... please? :)). One of those issues it that I'm trying to receive JSON for a POST request (and later on other requests, of course). This is, however, failing. This is my code:
view.py(我知道IF语句不是它的工作原理
view.py (I'm aware that that IF-statement is not how it works
elif request.method == 'POST':
received_json_data = json.loads(request.body.decode("utf-8"))
content = received_json_data['content']
if content != "":
worksheet.insert_row([content["date"], content["days"], content["firstname"], content["lastname"], content["team"], content["training"], content["company"], content["city"], content["cost"], content["invoice"], content["info"]], 1)
return JsonResponse(content, safe=False, status=status.HTTP_201_CREATED)
else:
return JsonResponse([], safe=False, status=status.HTTP_400_BAD_REQUEST)
为此编写的测试:
def test_post_all_training_returns_201_when_correct_format(self):
"""
A POST request on /trainings/ should create a new training
"""
headers = {'content-type': 'application/json'}
response = self.client.post('/trainings/', json=json.dumps(self.training_json_body), headers=headers, format="json")
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
training_json_body的内容:
The contents of the training_json_body:
self.training_json_body = {
"date": "1/1/2018",
"days": 1,
"firstname": "Data",
"lastname": "Data",
"team": "Data",
"training": "Data",
"company": "Data",
"city": "Data",
"cost": 1,
"invoice": "Data",
"info": "Data"
}
追踪
Traceback (most recent call last):
File "C:\Python\backend\unleashedapp\trainings\tests.py", line 64, in test_post_all_training_returns_201_when_correct_format
response = self.client.post('/trainings/', json=json.dumps(self.training_json_body), headers=headers, format="json")
File "C:\Users\yanni\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\test\client.py", line 525, in post
response = super().post(path, data=data, content_type=content_type, secure=secure, **extra)
File "C:\Users\yanni\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\test\client.py", line 341, in post
secure=secure, **extra)
File "C:\Users\yanni\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\test\client.py", line 404, in generic
return self.request(**r)
File "C:\Users\yanni\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\test\client.py", line 485, in request
raise exc_value
File "C:\Users\yanni\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
response = get_response(request)
File "C:\Users\yanni\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\yanni\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\yanni\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:\Python\backend\unleashedapp\trainings\views.py", line 36, in training_list
received_json_data = json.loads(request.body.decode("utf-8"))
File "C:\Users\yanni\AppData\Local\Programs\Python\Python36-32\lib\json\__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "C:\Users\yanni\AppData\Local\Programs\Python\Python36-32\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\yanni\AppData\Local\Programs\Python\Python36-32\lib\json\decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
我已经搜索了几天的解决方案,但无法使其正常工作,所以如果有人能够向正确的方向推动我,我将非常感激.
I have searched for a solution for a couple of days now but can't get it to work, so I would be really greatful if someone could push me in the right direction.
我已将代码更新为以下内容:
I have updated my code to the following:
class TrainingList(APIView):
def post(self, request, format=None):
sheet = request.GET.get('sheet', 'Data')
worksheet = spreadsheet.worksheet(sheet)
worksheet.append_row([request.data.get("date"), request.data.get("days"), request.data.get("firstname"), request.data.get("lastname"), request.data.get("team"), request.data.get("training"), request.data.get("company"), request.data.get("city"), request.data.get("cost"), request.data.get("invoice"), request.data.get("info")])
return JsonResponse("[]", safe=False, status=status.HTTP_201_CREATED)
这不再引发任何错误,但是append_row()函数现在为每个字段添加"None",因此显然数据仍然没有通过.我该如何解决?
This no longer throws any errors, but append_row() function is now adding "None" for every field, so clearly the data is still not going through. How can I fix this?
推荐答案
如果您使用的是Django REST框架,则可以通过访问 request.data
字典轻松地从请求对象中获取数据(更多信息在这里).
If you are using Django REST framework then you can easily get the data from the request object by accessing the request.data
dictionary (more info here).
如果您使用的是香草django视图,则可以通过使用请求对象并访问 request.POST ['< field_name>']
或`request.POST.get(").例如:
If you are using a vanilla django view then you can access POST data by using the request object and accessing request.POST['<field_name>']
or `request.POST.get(''). For example:
request.POST.get("date")
You can read more about that or look at examples here.
这篇关于使用REST Framework在Django中从POST获取JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!