1、认证的作用?
我们知道,当我们在网站上登陆之后,就会有自己的个人中心,之类的可以对自己的信息进行修改。但是http请求又是无状态的,所以导致我们每次请求都是一个新的请求,服务端每次都需要对请求进行认证,查看用户是否已经登陆,以及是谁登陆的。那我们服务器对服务器的每个请求进行认证的时候,不可能在每个视图中都写认证,一定是吧认证逻辑抽离出来,~~之前我们可以加装饰器,加中间件,那我们看看DRF框架是怎么帮我们做的!
2、认证怎么用
2.1 和版本控制一样,实在dispatch方法里执行了initial 方法。然后在那里便有我们的认证组件
2.2 进入我们的initial方法中,我们会发现这里边封装了认证组件,以及权限组件,频率组件,
2.3 点击perform_authentication进去,返回的是一个request.user。从大的Requset中进去
2.4 进去之后是
2.5 点击进入
3、使用方法
3.1 第一步,先创建表结构
from django.db import models class User(models.Model): name = models.CharField(max_length=32) pwd = models.CharField(max_length=32) token = models.UUIDField(null=True, blank=True) type = models.IntegerField(choices=((1, "普通用户"), (2, "vip"), (3, "svip")), default=1)