- # 自定义认证类
- class Authtication(object):
- # 自定义认证方法
- def authenticate(self, request):
- # 查看前端传过来的数据是否有 token
- token = request._request.GET.get('token')
- # 查询数据库用 token 匹配用户
- token_obj = models.UserToken.objects.filter(token=token).first()
- # 若没有匹配结果
- if not token_obj:
- # 抛出异常
- raise exceptions.AuthenticationFailed('请先登录')
- # 若有返回结果
- return (token_obj.user, token_obj)
- def authenticate_header(self,request):
- pass
- class OrderView(APIView):
- # 调用认证类, 保证若想调用该类中的方法, 必先通过认证
- authentication_classes = [Authtication,]
- def get(self,request):
- pass
来源: http://www.bubuko.com/infodetail-3280858.html