Javascript显然中断了与Django的连接

时间:2012-08-12 02:37:04

标签: javascript jquery django pipe broken-pipe

当javascript尝试重新加载页面时,以下错误会从Django发送给我的律师。

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

据我所知,这意味着谷歌浏览器会在服务器完成发送页面之前断开连接。为什么?我该如何解决?

这是javascript(Noty是一个确认框插件。只是知道我在其旁边添加“ * * ”的内容是实际得到的代码执行):

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); //*******
            }
            }
    ]});
}

这是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")

1 个答案:

答案 0 :(得分:1)

为什么在函数中使用print?除了在shell环境中,它们将无法打印任何内容。

我建议重写你的功能:

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

我建议尝试一下,如果没有提供ID或用户,这可能会清理您的问题。

此外,您是否在Draft()中使用ForeignKey作为user_id字段?如果是,您可以检查以下内容:user is Draft.objects.get(id = id).user_id

<强>更新

如果提供的id无效,django将抛出异常。你可能也希望抓住并处理它。

原谅我有限的jQuery体验,但不应该是{'id':id}吗?这就是我一直使用它的方式。