I am posting data through 127.0.0.8000/post/
. My post is of course successful cause I am able to see what I posted in the base URL of 127.0.0.8000
successfully but I get the following response on the Django front end.
JavaScript
x
3
1
AssertionError at /post/
2
Expected a `Response`, `HttpResponse` or `HttpStreamingResponse` to be returned from the view, but received a `<class 'NoneType'>`
3
In VS Code I get the following errors in terminal
JavaScript
1
18
18
1
Internal Server Error: /post/
2
Traceback (most recent call last):
3
File "C:UsersAhmedAppDataLocalProgramsPythonPython310libsite-packagesdjangocorehandlersexception.py", line 47, in inner
4
response = get_response(request)
5
File "C:UsersAhmedAppDataLocalProgramsPythonPython310libsite-packagesdjangocorehandlersbase.py", line 181, in _get_response
6
response = wrapped_callback(request, *callback_args, **callback_kwargs)
7
File "C:UsersAhmedAppDataLocalProgramsPythonPython310libsite-packagesdjangoviewsdecoratorscsrf.py", line 54, in wrapped_view
8
return view_func(*args, **kwargs)
9
File "C:UsersAhmedAppDataLocalProgramsPythonPython310libsite-packagesdjangoviewsgenericbase.py", line 69, in view
10
return self.dispatch(request, *args, **kwargs)
11
File "C:UsersAhmedAppDataLocalProgramsPythonPython310libsite-packagesrest_frameworkviews.py", line 511, in dispatch
12
self.response = self.finalize_response(request, response, *args, **kwargs)
13
File "C:UsersAhmedAppDataLocalProgramsPythonPython310libsite-packagesrest_frameworkviews.py", line 423, in finalize_response
14
assert isinstance(response, HttpResponseBase), (
15
AssertionError: Expected a `Response`, `HttpResponse` or `HttpStreamingResponse` to be returned from the view, but received a `<class 'NoneType'>`
16
[27/Feb/2022 16:22:13] "POST /post/ HTTP/1.1" 500 74030
17
[27/Feb/2022 16:22:24] "GET / HTTP/1.1" 200 5799
18
My Views.Py is the following
JavaScript
1
18
18
1
@api_view(['GET'])
2
def getData(request):
3
queryset = Wallet.objects.all()
4
serializer = WalletSerializer(queryset, many=True, context={'request': request})
5
return Response(serializer.data)
6
7
@api_view(['GET'])
8
def getSingleData(request,pk):
9
queryset = Wallet.objects.all(id=pk)
10
serializer = WalletSerializer(queryset, many=False)
11
return Response(serializer.data)
12
13
@api_view(['POST'])
14
def postData(request):
15
serializer = WalletSerializer(data = request.data)
16
if serializer.is_valid():
17
serializer.save()
18
P.S I am also unable to GET individual data through ID in the second method, it doesn’t work
Advertisement
Answer
You should return a response instance from your view/API
JavaScript
1
7
1
@api_view(['POST'])
2
def postData(request):
3
serializer = WalletSerializer(data=request.data)
4
if serializer.is_valid():
5
serializer.save()
6
return Response(serializer.data)
7
return Response(serializer.errors)