【问题标题】:AssertionError: Expected a `Response`, `HttpResponse` or `HttpStreamingResponse` to be returned from view, but received `<class 'NoneType'>`AssertionError:期望从视图返回一个`Response`、`HttpResponse`或`HttpStreamingResponse`,但收到`<class 'NoneType'>`
【发布时间】:2020-02-13 16:44:16
【问题描述】:

我有以下型号 -

class Userdetails(models.Model):

id = models.AutoField(primary_key=True)
name = models.CharField(max_length=200, blank=True, null=True)
userno = models.CharField(max_length=200, blank=True, null=True

和下面的视图 -

 @api_view(['GET', 'POST'])
    def useroptions(request):        # to create new user
        if request.method == 'GET':
            names = Userdetails.objects.values("name","userno","id")
            return Response(names)

        elif request.method == 'POST':
            count = Userdetails.objects.count()
            serializer = userdetailsSerializer(data=request.data)
            usernos = Userdetails.objects.values("userno")
            names = Userdetails.objects.values("name")
            list_of_names = []
            for ele in names:
                list_of_names.append(ele["name"])

            list_of_usernos = []
            for ele in usernos:
                list_of_usernos.append(ele["userno"])

这在 CMD 上给了我这个错误 -

Internal Server Error: /view/pardel/2/multiuser
Traceback (most recent call last):
  File "/home/hostbooks/django1/myproject/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/hostbooks/django1/myproject/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/hostbooks/django1/myproject/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/hostbooks/django1/myproject/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/hostbooks/django1/myproject/lib/python3.6/site-packages/django/views/generic/base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/hostbooks/django1/myproject/lib/python3.6/site-packages/rest_framework/views.py", line 507, in dispatch
    self.response = self.finalize_response(request, response, *args, **kwargs)
  File "/home/hostbooks/django1/myproject/lib/python3.6/site-packages/rest_framework/views.py", line 422, in finalize_response
    % type(response)
AssertionError: Expected a `Response`, `HttpResponse` or `HttpStreamingResponse` to be returned from the view, but received a `<class 'NoneType'>`
[13/Feb/2020 08:09:14] "POST /view/pardel/2/multiuser HTTP/1.1" 500 17234

我没有得到这个错误的原因,请我解决这个错误。

【问题讨论】:

    标签: python django python-3.x django-rest-framework django-views


    【解决方案1】:

    您没有为POST 请求返回http 响应对象。来自django docs

    您编写的每个视图都负责实例化、填充和 返回一个 HttpResponse。

    更改您的视图以返回发布请求的 http 响应。

    @api_view(['GET', 'POST'])
    def useroptions(request):        # to create new user
        if request.method == 'GET':
            names = Userdetails.objects.values("name","userno","id")
            return Response(names)
    
        elif request.method == 'POST':
            count = Userdetails.objects.count()
            serializer = userdetailsSerializer(data=request.data)
            usernos = Userdetails.objects.values("userno")
            names = Userdetails.objects.values("name")
            list_of_names = []
            for ele in names:
                list_of_names.append(ele["name"])
    
            list_of_usernos = []
            for ele in usernos:
                list_of_usernos.append(ele["userno"])
            context = {'data': serializer.data, 'names': list_of_names, 'usernos': list_of_usernos} # change it as per your requirement
            return Response(context)
    

    【讨论】:

      【解决方案2】:

      您收到此错误是因为您没有从视图中返回。

      代码应该是这样的。

      @api_view(['GET', 'POST'])
          def useroptions(request):        # to create new user
              if request.method == 'GET':
                  names = Userdetails.objects.values("name","userno","id")
                  return Response(names)
      
              elif request.method == 'POST':
                  count = Userdetails.objects.count()
                  serializer = userdetailsSerializer(data=request.data)
                  usernos = Userdetails.objects.values("userno")
                  names = Userdetails.objects.values("name")
                  list_of_names = []
                  for ele in names:
                      list_of_names.append(ele["name"])
      
                  list_of_usernos = []
                  for ele in usernos:
                      list_of_usernos.append(ele["userno"])
      
                  response_dict = {}
                  # update response_dict with whatever you want to send in response
                  return Response(response_dict)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-05-31
        • 2020-10-04
        • 2021-12-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-23
        • 2015-10-02
        相关资源
        最近更新 更多