发布成功但前端响应错误

发布于 2025-01-10 07:52:03 字数 2483 浏览 1 评论 0原文

我通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼中杀气 2025-01-17 07:52:03

您应该从您的视图/API 返回一个响应实例

@api_view(['POST'])
def postData(request):
    serializer = WalletSerializer(data=request.data)
    if serializer.is_valid():
        serializer.save()
        return Response(serializer.data)
    return Response(serializer.errors)

You should return a response instance from your view/API

@api_view(['POST'])
def postData(request):
    serializer = WalletSerializer(data=request.data)
    if serializer.is_valid():
        serializer.save()
        return Response(serializer.data)
    return Response(serializer.errors)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文