Javascript apparently breaking off connection with Django
-
25-06-2021 - |
문제
The following error is send to my counsel from Django when javascript tries to page reload.
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 86, in run
[11/Aug/2012 22:30:23] "GET /posting/drafts HTTP/1.1" 200 2785
self.finish_response()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 127, in finish_response
self.write(data)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 210, in write
self.send_headers()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 268, in send_headers
self.send_preamble()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 192, in send_preamble
'Date: %s\r\n' % format_date_time(time.time())
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 324, in write
self.flush()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 57954)
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 582, in process_request_thread
self.finish_request(request, client_address)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 323, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/Library/Python/2.7/site-packages/Django-1.4-py2.7.egg/django/core/servers/basehttp.py", line 139, in __init__
super(WSGIRequestHandler, self).__init__(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 640, in __init__
self.finish()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 693, in finish
self.wfile.flush()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
From what I understand, this means google chrome is breaking off the connection before the server finishes sending the page. Why? How can I fix it?
Here is the javascript (Noty is a confirm box plug in. Just know that the stuff that I put a "**" next to is code that actually gets executed):
function delete_draft(id, name) {
var text = 'Are you sure you want to delete "' + name + '"?'; //*********
var confirm = noty({
text: text,
layout: 'center',
type: 'confirm',
modal: true,
buttons: [
{addClass: 'btn btn-danger', text: 'Cancel', onClick: function($noty) {
$noty.close();
}
},
{addClass: 'btn btn-primary', text: 'Delete', onClick: function($noty) {
$.post('/ajax/drafts/delete', {id:id}, function(data) { //**********
document.location.reload(true); //*******
});
document.location.reload(true); //*******
}
}
]});
}
This is the view in Django:
def is_draft_owner(id, user): # It might be a coincidence, but when I added this the problems started.
print "test"
if user.pk is Draft.objects.get(id = id).user_id:
return True
print "checl"
print user.id
print Draft.objects.get(id = id).user_id
return False
def document_delete(request):
if is_draft_owner(request.POST['id'], request.user):
draft = Draft.objects.get(id = request.POST['id'])
draft.delete()
return HttpResponse("done")
해결책
Why do you use print
's inside a function? They will not be able to print anything, except inside a shell environment.
I would suggest rewriting your function to this:
def is_draft_owner(id = None, user = None):
if id and user:
return user.pk is Draft.objects.get(id = id).user_id
else:
return False
I suggest giving this a try, this might clean up your problem if no id or user was provided.
Furthermore, did you use a ForeignKey for your user_id field in Draft()? If so, you can check for the following: user is Draft.objects.get(id = id).user_id
.
Update:
If the id provided is invalid django will throw an exception. You might want to catch and handle that too.
Forgive my limited jQuery experience, but shouldn't it be {'id':id}
? That's how I always use it.