当前位置: 首页 >  聚焦  >  正文

Django REST Framework-视图的装饰器(二)_世界热资讯

来源:腾讯云   时间:2023-04-27 10:41:56


(资料图片仅供参考)

@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进行身份验证。这意味着只有提供正确的基本

X 关闭

Copyright ©  2015-2022 南方造纸网版权所有  备案号:粤ICP备18023326号-21   联系邮箱:855 729 8@qq.com