-1

Im trying to receive data in json format in django restframework and utilize the data immediately but i realize i get error that 'NoneType' object is not subscriptable' and i feel that i need to use cleaned_data but i dont know how to use cleaned_data in rest framework...here is my code:

@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)

Here is my traceback error:

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

[That is the image of the error im getting below][1] [1]: https://i.stack.imgur.com/wkOwa.png

Oladipo
  • 19
  • 4
  • You should include the code for `process_payment`. Why are you converting that outcome using `str`? Have you checked whether one of the four variables is `None` before being passed to `process_payment`? – Wouter Oct 11 '21 at 14:51
  • Did you try the suggestions [here](https://stackoverflow.com/questions/8949252/why-do-i-get-attributeerror-nonetype-object-has-no-attribute-something?r=SearchResults&s=2|143.5901), [here](https://stackoverflow.com/questions/18866657/nonetype-object-is-not-subscriptable?r=SearchResults&s=5|107.1203), or other posts with "NoneTpye" is not subscriptable? – marcel h Oct 11 '21 at 14:52
  • @Wouter when i included the code of `process_payment`, the error persisted...If i print the variables, im getting exactly what i inputed but still, it tells me that 'NoneType' object is not subscriptable' – Oladipo Oct 11 '21 at 15:10
  • @marcelh Ive checked the questions, ive checked djangorestframework docs still its not working – Oladipo Oct 11 '21 at 15:11
  • please add the complete error traceback – JPG Oct 11 '21 at 15:15
  • Can you paste the process_payment code? – LiquidDeath Oct 11 '21 at 21:01

2 Answers2

1

I have fixed it. I printed the response at the process payment to see the error message, so I got:

{'status': 'error', 'message': 'Invalid authorization key', 'data': None}

I now remember that I altered my secret key in the morning, so I got it from my dashboard and made the necessary corrections and everything started working.

Oladipo
  • 19
  • 4
0

Wrong answer: Just replace request.data to request.POST

Edited

I see your traceback and found that the problem is in line 55 of your views.py where you use response['data']. It is None!

mrash
  • 883
  • 7
  • 18