DRF用户权限和Django发送邮件、itsdangerous模块的使用
DRF的用户权限
看官方文档:https://www.django-rest-framework.org/api-guide/permissions/
全局设置权限
DEFAULT_PERMISSION_CLASSES用来设置默认权限策略
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
# 默认都需要登录验证
'rest_framework.permissions.IsAuthenticated',
)
}
如果未指定,则此设置默认为允许不受限制的访问:
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.AllowAny',
)
单个视图设置
APIView:
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
class ExampleView(APIView):
permission_classes = (IsAuthenticated,)
def get(self, request, format=None):
content = {
'status': 'request was permitted'
}
return Response(content)
或者,如果您使用的是@api_view具有基于功能的视图的装饰器。
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
@api_view(['GET'])
@permission_classes((IsAuthenticated, ))
def example_view(request, format=None):
content = {
'status': 'request was permitted'
}
return Response(content)
Django的发送邮件功能
需要开通邮箱里的POP3/SMTP服务,以qq邮箱为例:在设置-账户里开通