Javascript显然切断了与Django的连接

Javascript apparently breaking off connection with Django

本文关键字:Django 连接 Javascript      更新时间:2023-09-26

当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")
为什么在

函数中使用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() 中为您的user_id字段使用了外键?如果是这样,您可以检查以下内容:user is Draft.objects.get(id = id).user_id

更新:

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

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