Django-restframework 认证

【view层】

from rest_framework.authentication import BaseAuthentication
from rest_framework import exceptions


# 认证类
class MyAuthentication(object):
    def authenticate(self, request):
        # token = request._request.GET.get('token')
        name = request._request.GET.get('name')
        pwd = request._request.GET.get('pwd')
        '''
        # 获取用户名和密码,去校验数据
        这里写获取用户和校验代码
        '''
        print(name, pwd)

        # if not token:
        if not name and not pwd:
            raise exceptions.AuthenticationFailed('用户认证失败')
        return ('alex', None)
    # 这个方法是必写的,先这么写着
    def authenticate_header(self, xxx):
        pass


class DogView(APIView):
    # authentication_classes = [BaseAuthentication,]
    authentication_classes = [MyAuthentication,]
    def get(self, request, *args, **kwargs):
        res = {
            'code':1000,
            'msg':{'msg':'Dog'}
        }

        print('获取Dog')
        return HttpResponse(json.dumps(res))

    def post(self, requst, *args, **kwargs):
        return HttpResponse('提交Dog')

    def delete(self, request, *args, **kwargs):
        print('删除Dog')
        return HttpResponse('删除Dog')

    def put(self, request, *args, **kwargs):
        print('更新Dog')
        return HttpResponse('更新Dog')

【url路由配置】

urlpatterns = [
    path('dog/', DogView.as_view()),
]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值