发布成功但前端响应错误
我通过 127.0.0.8000/post/
发布数据。我的帖子当然是成功的,因为我能够成功地看到我在 127.0.0.8000
的基本 URL 中发布的内容,但我在 Django 前端收到以下响应。
AssertionError at /post/
Expected a `Response`, `HttpResponse` or `HttpStreamingResponse` to be returned from the view, but received a `<class 'NoneType'>`
中收到以下错误
Internal Server Error: /post/
Traceback (most recent call last):
File "C:\Users\Ahmed\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\Ahmed\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Ahmed\AppData\Local\Programs\Python\Python310\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:\Users\Ahmed\AppData\Local\Programs\Python\Python310\lib\site-packages\django\views\generic\base.py", line 69, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\Ahmed\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\views.py", line 511, in dispatch
self.response = self.finalize_response(request, response, *args, **kwargs)
File "C:\Users\Ahmed\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\views.py", line 423, in finalize_response
assert isinstance(response, HttpResponseBase), (
AssertionError: Expected a `Response`, `HttpResponse` or `HttpStreamingResponse` to be returned from the view, but received a `<class 'NoneType'>`
[27/Feb/2022 16:22:13] "POST /post/ HTTP/1.1" 500 74030
[27/Feb/2022 16:22:24] "GET / HTTP/1.1" 200 5799
在 VS Code 中,我在终端My Views.Py
@api_view(['GET'])
def getData(request):
queryset = Wallet.objects.all()
serializer = WalletSerializer(queryset, many=True, context={'request': request})
return Response(serializer.data)
@api_view(['GET'])
def getSingleData(request,pk):
queryset = Wallet.objects.all(id=pk)
serializer = WalletSerializer(queryset, many=False)
return Response(serializer.data)
@api_view(['POST'])
def postData(request):
serializer = WalletSerializer(data = request.data)
if serializer.is_valid():
serializer.save()
PS 我也无法在第二种方法中通过 ID 获取个人数据,它不起作用
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.
AssertionError at /post/
Expected a `Response`, `HttpResponse` or `HttpStreamingResponse` to be returned from the view, but received a `<class 'NoneType'>`
In VS Code I get the following errors in terminal
Internal Server Error: /post/
Traceback (most recent call last):
File "C:\Users\Ahmed\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\Ahmed\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Ahmed\AppData\Local\Programs\Python\Python310\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:\Users\Ahmed\AppData\Local\Programs\Python\Python310\lib\site-packages\django\views\generic\base.py", line 69, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\Ahmed\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\views.py", line 511, in dispatch
self.response = self.finalize_response(request, response, *args, **kwargs)
File "C:\Users\Ahmed\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\views.py", line 423, in finalize_response
assert isinstance(response, HttpResponseBase), (
AssertionError: Expected a `Response`, `HttpResponse` or `HttpStreamingResponse` to be returned from the view, but received a `<class 'NoneType'>`
[27/Feb/2022 16:22:13] "POST /post/ HTTP/1.1" 500 74030
[27/Feb/2022 16:22:24] "GET / HTTP/1.1" 200 5799
My Views.Py is the following
@api_view(['GET'])
def getData(request):
queryset = Wallet.objects.all()
serializer = WalletSerializer(queryset, many=True, context={'request': request})
return Response(serializer.data)
@api_view(['GET'])
def getSingleData(request,pk):
queryset = Wallet.objects.all(id=pk)
serializer = WalletSerializer(queryset, many=False)
return Response(serializer.data)
@api_view(['POST'])
def postData(request):
serializer = WalletSerializer(data = request.data)
if serializer.is_valid():
serializer.save()
P.S I am also unable to GET individual data through ID in the second method, it doesn't work
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该从您的视图/API 返回一个响应实例
You should return a response instance from your view/API