0

I've wrote a method in my views.py as a api for user login. (using django 2.1) it's completely working with POSTMAN, but when I try to use ajax to send request to this api, it's kind of work but instead of returning jsonresponse with proper status code, I have this strange error in my server console.

--edit: I don't understand, How it is working with postman but not ajax? what is the difference?! ajax causes that error?! something wrong with ajax?

This is Error:

[23/Dec/2018 21:40:46] "POST /api/login/ HTTP/1.1" 500 59
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 56813)
Traceback (most recent call last):
  File "c:\python37\Lib\wsgiref\handlers.py", line 138, in run
    self.finish_response()
  File "c:\python37\Lib\wsgiref\handlers.py", line 180, in finish_response
    self.write(data)
  File "c:\python37\Lib\wsgiref\handlers.py", line 274, in write
    self.send_headers()
  File "c:\python37\Lib\wsgiref\handlers.py", line 332, in send_headers
    self.send_preamble()
  File "c:\python37\Lib\wsgiref\handlers.py", line 255, in send_preamble
    ('Date: %s\r\n' % format_date_time(time.time())).encode('iso-8859-1')
  File "c:\python37\Lib\wsgiref\handlers.py", line 453, in _write
    result = self.stdout.write(data)
  File "c:\python37\Lib\socketserver.py", line 796, in write
    self._sock.sendall(b)
ConnectionAbortedError: [WinError 10053] An established connection was aborted b
y the software in your host machine

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\python37\Lib\wsgiref\handlers.py", line 141, in run
    self.handle_error()
  File "C:\Users\crash\Envs\user_auth\lib\site-packages\django\core\servers\base
http.py", line 98, in handle_error
    super().handle_error()
  File "c:\python37\Lib\wsgiref\handlers.py", line 368, in handle_error
    self.finish_response()
  File "c:\python37\Lib\wsgiref\handlers.py", line 180, in finish_response
    self.write(data)
  File "c:\python37\Lib\wsgiref\handlers.py", line 274, in write
    self.send_headers()
  File "c:\python37\Lib\wsgiref\handlers.py", line 331, in send_headers
    if not self.origin_server or self.client_is_modern():
  File "c:\python37\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:\python37\Lib\socketserver.py", line 647, in process_request_thread
    self.finish_request(request, client_address)
  File "c:\python37\Lib\socketserver.py", line 357, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "c:\python37\Lib\socketserver.py", line 717, in __init__
    self.handle()
  File "C:\Users\crash\Envs\user_auth\lib\site-packages\django\core\servers\base
http.py", line 153, in handle
    self.handle_one_request()
  File "C:\Users\crash\Envs\user_auth\lib\site-packages\django\core\servers\base
http.py", line 176, in handle_one_request
    handler.run(self.server.get_app())
  File "c:\python37\Lib\wsgiref\handlers.py", line 144, in run
    self.close()
  File "c:\python37\Lib\wsgiref\simple_server.py", line 35, in close
    self.status.split(' ',1)[0], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'

My views.py code:

@require_POST
def user_login(request):
    data = json.loads(request.body.decode('utf-8'))
    phone_number = data['phone_number']
    password = data['password']
    user = authenticate(phone_number=phone_number, password=password)
    if user is None:
        message = 'The username or password is wrong.'
        print(message)
        return JsonResponse({'message':message},status=406)
    if not user.two_step_auth:
        login(request,user)
        message='successfully loged in'
        print(message)
        return JsonResponse({'message':message}, status=200)
    gg = generate_activation_code(phone_number)
    if gg:
        message='The Activation code has been sent to your phone'
        print(message)
        return JsonResponse({'message':message}, status=200)
    return JsonResponse({}, status=400)

frontEnd ajax code :

    function send_user_info(){
        var phone_number = document.getElementById('input_tel').value
        var password = document.getElementById('input_password').value
        var dict = {"phone_number" : phone_number, "password" : password}
        $.ajax({
            url:'http://127.0.0.1:8000/api/login/',
            type: "post", // or "get"
            dataType: "json",
            contentType: "application/json",
            data: JSON.stringify(dict),
            success: function(data) {
            alert("Sucsessfully loged in.");
            },

        });
  }

what is wrong with this situation. please help.

Ali Crash
  • 458
  • 2
  • 5
  • 15
  • Possible duplicate of [Python: Attribute Error - 'NoneType' object has no attribute 'something'](https://stackoverflow.com/questions/8949252/python-attribute-error-nonetype-object-has-no-attribute-something) – Patrick Artner Dec 23 '18 at 19:15
  • 1
    Possibly this issue is related to a bug: https://bugs.python.org/issue27682, maybe consider using docker or any other virtualization until this issue is resolved – ruddra Dec 24 '18 at 05:19

1 Answers1

0

I had same issue when was using socketserver. Seems like there is some Windows related bug at connection. Try to use django-channels if you need websockets or asyncronous functionality. Worked for me.

Dan Zaikin
  • 197
  • 3
  • 14
  • if it's windows related bug, why postman doesn't raise any error when connecting? – Ali Crash Jan 08 '19 at 23:56
  • I can't say it without more information about postman and ajax requests. You can compare request details from them to see the difference. According to traceback, problems started when `RequestHandlerClass` instance appeared to have `None` instead of `status` which is not problem of view. Also, ajax code looks ok. – Dan Zaikin Jan 09 '19 at 00:40