0

当 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")
4

1 回答 1

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

更新:

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

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

于 2012-08-12T07:36:50.170 回答