장고 휴식 프레임 워크를 사용하여 API를 빌드 중이지만 기본 인증을 사용하여 요청을 인증하는 데 문제가 있습니다. 테스트를 위해 내가 POST
통해 호출하고 User
모델에 대한 유효한 사용자 이름/암호를 제공 뷰를 가지고 id=1018
Django REST 프레임 워크 인증 정보 없음
curl -X POST http://<my-url>/api/detail/1018 -u <username>:<password>
{"detail": "Authentication credentials were not provided."}
GET
요청이 잘 작동이 경우, 제공된 id
과 그 모델을 반환해야 인증이 필요 없기 때문입니다. 나는 대략 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
에 있습니다.