Python AssertionError:预期从视图返回'Response'、'HttpResponse'或'HttpStreamingResponse',但收到`<;类别';非类型'&燃气轮机`

Python AssertionError:预期从视图返回'Response'、'HttpResponse'或'HttpStreamingResponse',但收到`<;类别';非类型'&燃气轮机`,python,django,python-3.x,django-rest-framework,django-views,Python,Django,Python 3.x,Django Rest Framework,Django Views,我有以下型号- 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'])

我有以下型号-

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
内部服务器错误:/view/pardel/2/multiuser
回溯(最近一次呼叫最后一次):
文件“/home/hostbooks/djang1/myproject/lib/python3.6/site packages/django/core/handlers/exception.py”,第34行,在内部
响应=获取响应(请求)
文件“/home/hostbooks/djang1/myproject/lib/python3.6/site packages/django/core/handlers/base.py”,第115行,在“get”响应中
response=self.process\u异常\u由\u中间件(e,请求)
文件“/home/hostbooks/djang1/myproject/lib/python3.6/site packages/django/core/handlers/base.py”,第113行,在“get”响应中
响应=包装的回调(请求,*回调参数,**回调参数)
文件“/home/hostbooks/djang1/myproject/lib/python3.6/site packages/django/views/decorators/csrf.py”,包装视图中的第54行
返回视图函数(*args,**kwargs)
文件“/home/hostbooks/djang1/myproject/lib/python3.6/site packages/django/views/generic/base.py”,第71行,在视图中
返回自调度(请求,*args,**kwargs)
文件“/home/hostbooks/django1/myproject/lib/python3.6/site packages/rest_framework/views.py”,第507行,已发送
self.response=self.finalize_响应(请求、响应、*args、**kwargs)
文件“/home/hostbooks/django1/myproject/lib/python3.6/site packages/rest\u framework/views.py”,第422行,在finalize\u响应中
%类型(响应)
AssertionError:期望从视图返回'Response'、'HttpResponse'或'HttpStreamingResponse',但收到一个``
[13/Feb/2020 08:09:14]“POST/view/pardel/2/multiuser HTTP/1.1”500 17234

我不知道这个错误的原因,请让我来解决这个错误

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

代码应该是这样的

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

您没有为
POST
请求返回
http
响应对象。从:

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

更改视图以返回post请求的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)
@api_视图(['GET','POST']))
def useroptions(请求):#创建新用户
如果request.method==“GET”:
name=Userdetails.objects.values(“name”、“userno”、“id”)
返回响应(名称)
elif request.method==“POST”:
count=Userdetails.objects.count()
序列化程序=userdetailsSerializer(数据=request.data)
usernos=Userdetails.objects.values(“userno”)
名称=Userdetails.objects.values(“名称”)
名称列表=[]
对于名称中的元素:
名称列表。附加(ele[“name”])
用户编号列表=[]
对于usernos中的ele:
附加(ele[“userno”])的用户号列表
context={'data':serializer.data,'names':list_of_names,'usernos':list_of_usernos}根据需要更改它
返回响应(上下文)