rest framework错误笔记——身份验证和权限
生活随笔
收集整理的這篇文章主要介紹了
rest framework错误笔记——身份验证和权限
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
按照官網教程(http://www.django-rest-framework.org/tutorial/4-authentication-and-permissions/)走到最后一步驗證時,命令窗口執行http POST http://127.0.0.1:8000/snippets/ code="print 123"后
{"detail": "Authentication credentials were not provided." }這條結果是因為#視圖類中的寫法(1):
authentication_classes = ( BasicAuthentication, ) permission_classes = ( IsAuthenticatedOrReadOnly, )若根據教程的寫法(2)
permission_classes = (permissions.IsAuthenticatedOrReadOnly,IsOwnerOrReadOnly,)則返回如下結果:
{"detail": "You do not have permission to perform this action." }根據版本不同視圖類中的代碼不同則提示不同,雖然無論哪種結果都代表沒有權限,但還是建議用寫法(1),因為寫法(2)會導致執行http -a admin:password123 POST http://127.0.0.1:8000/snippets/ code="print 789"后仍然驗證不通過返回上面的結果。
當這些設置完成后,執行http -a admin:password123 POST http://127.0.0.1:8000/snippets/ code="print 789"報錯:
Exception Type: IntegrityError at /snippets/ Exception Value: NOT NULL constraint failed: snippets_snippet.owner_id只需將視圖類中post方法里面的
serializer.save()改為:
serializer.save(owner=self.request.user)?
轉載于:https://www.cnblogs.com/youleng/p/9183849.html
總結
以上是生活随笔為你收集整理的rest framework错误笔记——身份验证和权限的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Bing Speech Recognit
- 下一篇: Outlook Express 错误代码