I'm trying to implement a live search through the jquery plugin Select2 in my Django 1.11.4 project. Running Python 3.6. When I type in the textbox, the server doesn't seem to be able to handle the number of requests and it closes, followed by these series of errors. I've spent a couple hours following a couple threads on SO SO(more) , Google, etc but none have a working solution. I thought it was a python version issue, but I've upgraded from 2.7 to 3.6 and still have it. Any help would be great thanks!
This the view that's being called by the ajax call:
def directory_search(request):
# Query text from search bar
query = request.GET.get('q', None)
if query:
# Searches the DB for matching value
customers = Customer.objects.filter(
Q(name__icontains= query)|
Q(contract_account__icontains= query)|
Q(address__icontains= query)|
Q(address__icontains= query)|
Q(email__icontains= query)
).distinct()
# Turns queryset into dict
customers = customers.values("contract_account","name")
# Turns dict into list
customers_list = list(customers)
return JsonResponse(customers_list, safe=False)
else:
return JsonResponse(data={'success': False,'errors': 'No mathing items found'})
This is the js/ajax call for the Select2 plugin:
$(".customer-search-bar").select2({
ajax:{
dataType: 'json',
type: 'GET',
url:"{% url 'portal:directory_search' %}",
data: function (params) {
var queryParameters = {
q: params.term
}
return queryParameters;
},
processResults: function (data) {
return {
results: $.map(data, function (item) {
return {
text: item.name,
id: item.contract_account
}
})
};
}
},
escapeMarkup: function (markup) { return markup; },
minimumInputLength: 1,
templateResult: formatRepo,
templateSelection: formatRepoSelection,
language: { errorLoading:function(){ return "Searching..." }}
});
Errors: (I just broke them apart to make it more legible, but they occurred in the presented order)
1.
Traceback (most recent call last):
File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 138, in run
self.finish_response()
File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 180, in finish_response
self.write(data)
File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 274, in write
self.send_headers()
File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 332, in send_headers
self.send_preamble()
File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\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\leep\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 453, in _write
result = self.stdout.write(data)
File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\socketserver.py", line 775, in write
self._sock.sendall(b)
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine
2.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 141, in run
self.handle_error()
File "C:\Users\leep\PythonStuff\virtual_environments\rpp_3.6\lib\site-packages\django\core\servers\basehttp.py", line 88, in handle_error
super(ServerHandler, self).handle_error()
File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 368, in handle_error
self.finish_response()
File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 180, in finish_response
self.write(data)
File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 274, in write
self.send_headers()
File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 331, in send_headers
if not self.origin_server or self.client_is_modern():
File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\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
3.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\socketserver.py", line 639, in process_request_thread
self.finish_request(request, client_address)
File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\socketserver.py", line 361, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\socketserver.py", line 696, in __init__
self.handle()
File "C:\Users\leep\PythonStuff\virtual_environments\rpp_3.6\lib\site-packages\django\core\servers\basehttp.py", line 155, in handle
handler.run(self.server.get_app())
File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 144, in run
self.close()
File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\simple_server.py", line 35, in close
self.status.split(' ',1)[0], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'