2

我认为“document_delete”称为“is_draft owner”:

这是“is_draft_owner”:

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

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

这是页面的视图:

@login_required
def posting_draft(request):
    user = request.user
    user_drafts = Draft.objects.filter(user = user) # Order by date last oppened
    drafts = dict()
    for d in user_drafts:
        drafts[d.title] = d.id
    alertnum = get_alertnum(user)
    return render_to_response('Posting/Pages/posting_draft.html', {'STATIC_URL':STATIC_URL, 'draft_l' : drafts, 'selected':"dr", 'alertnum': alertnum})

不知何故,当我不调用“is_draft_owner”时,一切正常。当我调用它时,我得到了这个:

    [12/Aug/2012 00:53:45] "GET /posting/drafts HTTP/1.1" 200 2783
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 86, in run
    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', 49948)
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 文件:

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

我认为,但不知道,该页面正在调用删除文档,但随后在重新加载时中断。当我使用谷歌浏览器时,从日志中这似乎是有道理的(因为当我再次重新加载时,它已被删除),但对于 Firefox 来说似乎并非如此。我正在运行开发服务器,但这些问题仍然存在,我无法将它们关闭(有人说这是开发服务器的问题,但我什至不能在这样的情况下进行开发)。

4

1 回答 1

1

我将对您的代码发表一些评论;但我认为您应该使用发送的确切错误来修改问题;正如我在评论中提到的。

我会编辑你的代码 - 希望评论就足够了:

# Here you mean to check for equality, `is` checks for identity
# Also, you want the return of the comparison; so you don't need
# an else.

def is_draft_owner(id = None, user = None):
    if id and user:
        return user.pk == Draft.objects.get(id = id).user_id

# You are POST-ing to this view, but
# you need to exempt it from csrf checks
# or edit your javascript to make sure it
# sends the csrf cookie.
def document_delete(request):
    if request.method == 'POST':
        if is_draft_owner(request.POST['id'], request.user):
            # Get raises an exception that you are not catching:
            try:
                 draft = Draft.objects.get(id = request.POST['id'])
            except Draft.DoesNotExist:
                 # Do something

            # OR you can do this
            draft = get_object_or_404(Draft,id=request.POST['id'])
            draft.delete()
            return HttpResponse("done")
    else:
         return HttpResponse('Not Allowed')
于 2012-08-12T14:16:41.703 回答