Django error - matching query does not exist

后端 未结 4 1221
礼貌的吻别
礼貌的吻别 2020-12-24 11:25

I finally released my project to the production level and suddenly I have some issues I never had to deal with in the development phase.

When the users posts some ac

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-24 11:59

    your line raising the error is here:

    comment = Comment.objects.get(pk=comment_id)
    

    you try to access a non-existing comment.

    from django.shortcuts import get_object_or_404
    
    comment = get_object_or_404(Comment, pk=comment_id)
    

    Instead of having an error on your server, your user will get a 404 meaning that he tries to access a non existing resource.

    Ok up to here I suppose you are aware of this.

    Some users (and I'm part of them) let tabs running for long time, if users are authorized to delete data, it may happens. A 404 error may be a better error to handle a deleted resource error than sending an email to the admin.

    Other users go to addresses from their history, (same if data have been deleted since it may happens).

提交回复
热议问题