【資料圖】
@authentication_classes
@authentication_classes是Django REST Framework中的另一個常用的視圖裝飾器。它允許您指定在請求被處理之前必須通過的身份驗證類。如果請求不通過身份驗證,則將返回HTTP 401 Unauthorized錯誤。
下面是一個使用@authentication_classes裝飾器的示例:
from rest_framework.decorators import api_view, authentication_classesfrom rest_framework.authentication import BasicAuthenticationfrom rest_framework.response import Response@api_view(["GET"])@authentication_classes([BasicAuthentication])def my_view(request): data = {"message": "Hello, world!"} return Response(data)
在這個示例中,我們使用@authentication_classes裝飾器將my_view函數轉換為API視圖,并指定了需要使用BasicAuthentication進行身份驗證。這意味著只有提供正確的基本@authentication_classes
@authentication_classes是Django REST Framework中的另一個常用的視圖裝飾器。它允許您指定在請求被處理之前必須通過的身份驗證類。如果請求不通過身份驗證,則將返回HTTP 401 Unauthorized錯誤。
下面是一個使用@authentication_classes裝飾器的示例:
from rest_framework.decorators import api_view, authentication_classesfrom rest_framework.authentication import BasicAuthenticationfrom rest_framework.response import Response@api_view(["GET"])@authentication_classes([BasicAuthentication])def my_view(request): data = {"message": "Hello, world!"} return Response(data)
在這個示例中,我們使用@authentication_classes裝飾器將my_view函數轉換為API視圖,并指定了需要使用BasicAuthentication進行身份驗證。這意味著只有提供正確的基本
關鍵詞: