0
我使用Django rest框架構建了一個API,但我在使用基本認證來驗證請求時遇到了問題。出於測試目的,我有一個,當通過POST
打來電話,提供了User
模型有效的用戶名/密碼查看,應該返回模型所提供的id
,在這種情況下id=1018
Django REST框架沒有認證細節
curl -X POST http://<my-url>/api/detail/1018 -u <username>:<password>
{"detail": "Authentication credentials were not provided."}
GET
要求做工精細因爲不需要認證。我一直在大致遵循rest framework tutorial。在每一節課我都permission_classes = (permissions.IsAuthenticatedOrReadOnly, IsOwnerOrReadOnly,)
而且在permissions.py
from rest_framework import permissions
class IsOwnerOrReadOnly(permissions.BasePermission):
def has_object_permission(self, request, view, obj):
# Read permissions are allowed to any request,
# so we'll always allow GET, HEAD or OPTIONS requests.
if request.method in permissions.SAFE_METHODS:
return True
# Write permissions are only allowed to the owner of the account.
# I have commented this out and set it to return True so that I can test
# more easily narrow down the cause of the error.
#obj.owner.id == request.user.id
return True
我甚至不知道這是否是一個代碼問題,因爲它說,憑據沒有提供,而不是憑據是錯誤的。