'NoneType' 对象在 django rest 框架中不可订阅
'NoneType' object is not subscriptable in django rest framework
我试图在 django restframework 中接收 json 格式的数据并立即使用数据,但我意识到我收到错误消息 'NoneType' 对象不可订阅',我觉得我需要使用cleaned_data 但我不知道如何在 rest 框架中使用 cleaned_data...这是我的代码:
@api_view(['GET', "POST"])
def home(request):
if request.method == 'POST':
name = request.data.get('name')
email = request.data.get("email")
amount = request.data.get("amount")
phone = request.data.get("phone")
return redirect(str(process_payment(name, email, amount, phone)))
else:
responseData = {
"Homepage": "Payment"
}
return Response(responseData)
这是我的回溯错误:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/core/handlers/base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/views/generic/base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/rest_framework/decorators.py", line 50, in handler
return func(*args, **kwargs)
File "/Users/williamdapper/Desktop/payment/electronics/views.py", line 21, in home
return redirect(process_payment(name, email, amount, phone))
File "/Users/williamdapper/Desktop/payment/electronics/views.py", line 55, in process_payment
link=response['data']['link']
TypeError: 'NoneType' object is not subscriptable
[11/Oct/2021 15:19:25] "POST / HTTP/1.1" 500 95799
[这是我得到的错误图片][1]
[1]: https://i.stack.imgur.com/wkOwa.png
错误答案:只需将 request.data
替换为 request.POST
已编辑
我看到你的追溯,发现问题出在你的 views.py
的第 55 行你使用 response['data']
的地方。是None!
我已经修好了。我在process payment打印response看到报错信息,所以得到:
{'status': 'error', 'message': 'Invalid authorization key', 'data': None}
我现在记得我在早上更改了我的密钥,所以我从我的仪表板获取它并进行了必要的更正,然后一切都开始工作了。
我试图在 django restframework 中接收 json 格式的数据并立即使用数据,但我意识到我收到错误消息 'NoneType' 对象不可订阅',我觉得我需要使用cleaned_data 但我不知道如何在 rest 框架中使用 cleaned_data...这是我的代码:
@api_view(['GET', "POST"])
def home(request):
if request.method == 'POST':
name = request.data.get('name')
email = request.data.get("email")
amount = request.data.get("amount")
phone = request.data.get("phone")
return redirect(str(process_payment(name, email, amount, phone)))
else:
responseData = {
"Homepage": "Payment"
}
return Response(responseData)
这是我的回溯错误:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/core/handlers/base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/views/generic/base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/rest_framework/decorators.py", line 50, in handler
return func(*args, **kwargs)
File "/Users/williamdapper/Desktop/payment/electronics/views.py", line 21, in home
return redirect(process_payment(name, email, amount, phone))
File "/Users/williamdapper/Desktop/payment/electronics/views.py", line 55, in process_payment
link=response['data']['link']
TypeError: 'NoneType' object is not subscriptable
[11/Oct/2021 15:19:25] "POST / HTTP/1.1" 500 95799
[这是我得到的错误图片][1] [1]: https://i.stack.imgur.com/wkOwa.png
错误答案:只需将 request.data
替换为 request.POST
已编辑
我看到你的追溯,发现问题出在你的 views.py
的第 55 行你使用 response['data']
的地方。是None!
我已经修好了。我在process payment打印response看到报错信息,所以得到:
{'status': 'error', 'message': 'Invalid authorization key', 'data': None}
我现在记得我在早上更改了我的密钥,所以我从我的仪表板获取它并进行了必要的更正,然后一切都开始工作了。