首页 > 解决方案 > 获取 Python Django 'SERVER_PROTOCOL' TypeError:'NoneType' 对象不可下标

问题描述

我编写了 PUT 请求 API 来设置用户密码。我写了这么多视图,但我没有遇到像下面的视图功能这样的问题。

视图.py

@api_view(['PUT'])
@permission_classes([])
def setForgotPasswordWithNewPassword(request):
    data = request.data
    print(data)
    user = User.objects.get(username=data['username'])
    secret_key = request.data['secretkey']
    backend_secretkey = cache.get('reset_pwd_%s' % (data['username']))
    if backend_secretkey:
        if backend_secretkey == secret_key:
            if user:
                user.set_password(data['password'])
                # print(user.password)
                user.save()
                return Response({"msg":"Your password has been successfully changed"},status=200)
            else:
                return Response({"error":"Username does not exist on our system"},status=403)
        else:
            return Response({"error": "Your not authorized to use this link"}, status=403)
    else:
        return Response({"error": "Your password link has been expired, please try again"}, status=403)

使用 Postman,我在命令行上没有收到任何异常。但是当我使用我的 Angular 项目访问这个 API 时,得到了以下异常,并且由于再次加载了这个异常 wsgi 服务器,所以再次加载到首页。但更改密码有效。所以我很想知道我遇到的这个错误。

错误:

[15/Jun/2020 00:34:01] "PUT /api/setForgotPasswordWithNewPwd/ HTTP/1.1" 200 53
Traceback (most recent call last):
  File "C:\Users\vipin\AppData\Local\Programs\Python\Python36\lib\wsgiref\handlers.py", line 138, in run
    self.finish_response()
  File "C:\Users\vipin\AppData\Local\Programs\Python\Python36\lib\wsgiref\handlers.py", line 180, in finish_response
    self.write(data)
  File "C:\Users\vipin\AppData\Local\Programs\Python\Python36\lib\wsgiref\handlers.py", line 274, in write
    self.send_headers()
  File "C:\Users\vipin\AppData\Local\Programs\Python\Python36\lib\wsgiref\handlers.py", line 332, in send_headers
    self.send_preamble()
  File "C:\Users\vipin\AppData\Local\Programs\Python\Python36\lib\wsgiref\handlers.py", line 255, in send_preamble
    ('Date: %s\r\n' % format_date_time(time.time())).encode('iso-8859-1')
  File "C:\Users\vipin\AppData\Local\Programs\Python\Python36\lib\wsgiref\handlers.py", line 453, in _write
    result = self.stdout.write(data)
  File "C:\Users\vipin\AppData\Local\Programs\Python\Python36\lib\socketserver.py", line 803, in write
    self._sock.sendall(b)
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine
[15/Jun/2020 00:34:01] "PUT /api/setForgotPasswordWithNewPwd/ HTTP/1.1" 500 59
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 49954)
Traceback (most recent call last):
  File "C:\Users\vipin\AppData\Local\Programs\Python\Python36\lib\wsgiref\handlers.py", line 138, in run
    self.finish_response()
  File "C:\Users\vipin\AppData\Local\Programs\Python\Python36\lib\wsgiref\handlers.py", line 180, in finish_response
    self.write(data)
  File "C:\Users\vipin\AppData\Local\Programs\Python\Python36\lib\wsgiref\handlers.py", line 274, in write
    self.send_headers()
  File "C:\Users\vipin\AppData\Local\Programs\Python\Python36\lib\wsgiref\handlers.py", line 332, in send_headers
    self.send_preamble()
  File "C:\Users\vipin\AppData\Local\Programs\Python\Python36\lib\wsgiref\handlers.py", line 255, in send_preamble
    ('Date: %s\r\n' % format_date_time(time.time())).encode('iso-8859-1')
  File "C:\Users\vipin\AppData\Local\Programs\Python\Python36\lib\wsgiref\handlers.py", line 453, in _write
    result = self.stdout.write(data)
  File "C:\Users\vipin\AppData\Local\Programs\Python\Python36\lib\socketserver.py", line 803, in write
    self._sock.sendall(b)
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\vipin\AppData\Local\Programs\Python\Python36\lib\wsgiref\handlers.py", line 141, in run
    self.handle_error()
  File "C:\Users\vipin\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\servers\basehttp.py", line 119, in handle_error
    super().handle_error()
  File "C:\Users\vipin\AppData\Local\Programs\Python\Python36\lib\wsgiref\handlers.py", line 368, in handle_error
    self.finish_response()
  File "C:\Users\vipin\AppData\Local\Programs\Python\Python36\lib\wsgiref\handlers.py", line 180, in finish_response
    self.write(data)
  File "C:\Users\vipin\AppData\Local\Programs\Python\Python36\lib\wsgiref\handlers.py", line 274, in write
    self.send_headers()
  File "C:\Users\vipin\AppData\Local\Programs\Python\Python36\lib\wsgiref\handlers.py", line 331, in send_headers
    if not self.origin_server or self.client_is_modern():
  File "C:\Users\vipin\AppData\Local\Programs\Python\Python36\lib\wsgiref\handlers.py", line 344, in client_is_modern
    return self.environ['SERVER_PROTOCOL'].upper() != 'HTTP/0.9'
TypeError: 'NoneType' object is not subscriptable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\vipin\AppData\Local\Programs\Python\Python36\lib\socketserver.py", line 654, in process_request_thread
    self.finish_request(request, client_address)
  File "C:\Users\vipin\AppData\Local\Programs\Python\Python36\lib\socketserver.py", line 364, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:\Users\vipin\AppData\Local\Programs\Python\Python36\lib\socketserver.py", line 724, in __init__
    self.handle()
  File "C:\Users\vipin\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\servers\basehttp.py", line 174, in handle
    self.handle_one_request()
  File "C:\Users\vipin\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\servers\basehttp.py", line 197, in handle_one_request
    handler.run(self.server.get_app())
  File "C:\Users\vipin\AppData\Local\Programs\Python\Python36\lib\wsgiref\handlers.py", line 144, in run
    self.close()
  File "C:\Users\vipin\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\servers\basehttp.py", line 114, in close
    super().close()
  File "C:\Users\vipin\AppData\Local\Programs\Python\Python36\lib\wsgiref\simple_server.py", line 35, in close
    self.status.split(' ',1)[0], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'

使用邮递员工具调用没有像上面那样出现任何异常

我的 Angular 调用:

setForgotPassword(data):Observable<any>{
    return this.http.put<any>(this._base_url+this._setForgotPassword_url,data)
  }

打字稿:

setPassword(secretkey){
    this.data.secretkey = secretkey
    console.log(this.data)
    this.usr_srv.setForgotPassword(this.data).subscribe(
      res => {
        console.log(res)
        // let dialogRef = this.dialog.open(DialogDataExampleDialog,{
        //   data: {msg: 'Your password has been successfully set',color:"green"}
        // });
        // dialogRef.afterClosed().subscribe(result => {
        //   this._router.navigate(['/login'])
        // });
      },
      err =>{
        console.log(err)
        // let dialogRef = this.dialog.open(DialogDataExampleDialog,{
        //   data: {msg: err.error.error,color:"red"}
        // });
        // dialogRef.afterClosed().subscribe(result => {
        //   this._router.navigate(['/login'])
        // });
      }
    )
  }

标签: pythondjangoangulardjango-rest-framework

解决方案


推荐阅读