我正在尝试使用Gspread在Django中为Google电子表格构建一个REST API,我遇到了很多问题(所以如果有人碰巧有一个完整的例子,请随时分享.. 。 请? :))。其中一个问题是我尝试接收POST请求的JSON(当然还有其他请求)。然而,这是失败的。这是我的代码:
view.py(我知道IF语句不是它的工作方式
$days = current($days);
while($days <= 66){
echo $days;
next ($days);
if (end ($days)){
reset($days);
}
为此编写的测试:
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)
training_json_body的内容:
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)
回溯
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)
这不再引发任何错误,但是append_row()函数现在正在添加&#34;无&#34;对于每个领域,显然数据仍未通过。我该如何解决这个问题?
答案 0 :(得分:1)
如果您正在使用Django REST框架,那么您可以通过访问double* get_real() const {
return real;
}
字典(more info here)轻松地从请求对象获取数据。
如果您使用的是vanilla django视图,则可以使用请求对象访问POST数据并访问request.data
或`request.POST.get('')。例如:
request.POST['<field_name>']
您可以阅读更多相关内容或查看示例here。
答案 1 :(得分:0)
尝试一下:
request.data.get("sheet")
答案 2 :(得分:0)
要获取Json格式的参数,您需要将请求的子方法从“ GET”更改为“ DATA”,如下所示:
sheet = request.data.get("sheet")