首页 技术 正文
技术 2022年11月18日
0 收藏 379 点赞 2,511 浏览 2930 个字

1. 简单演示,创建一个models的数据库表

class User(models.Model):
name=models.CharField(max_length=32)
pwd=models.CharField(max_length=32)
choice=((1,'超级用户'),(2,'普通用户'),(3,'穷逼用户'))
type=models.IntegerField(choices=choice,null=True)
# typ=models.ForeignKey(to='Type')# class Type(models.Model):
# id=models.AutoField(primary_key=True)
# name=models.CharField()class UserToken(models.Model):
token = models.CharField(max_length=64)
user=models.OneToOneField(to='User')

2. 编写一个角色权限的方法

#权限类
from rest_framework.permissions import BasePermission
class MyPermissions(BasePermission):
# message='你是2b'
def has_permission(self,request,view):
#代表是超级用户
if request.user.type ==1:
#如何去除type对应的文字 get_字段名_display()
user_str=request.user.get_type_display()
print(user_str) #超级用户,校验通过,返回true,校验失败,返回false
return True
else: return False

3. 演示调用方法

from rest_framework.request import Request
from rest_framework.authentication import SessionAuthentication
# from app01.MyAuth import MyAuthetication
from app01.MyAuth import MyPermissions
from app01.MyAuth import MyThrottling
class BooksView(APIView):
# authentication_classes=[MyAuthetication]
# permission_classes=[MyPermissions]
permission_classes=[]
throttle_classes=[MyThrottling]
def get(self,request):
#request.user就是当前登录用户
print(request.user)
print(request.auth)
return Response('ok')

4. 如何全局使用呢?

# 认证的全局配置
REST_FRAMEWORK={
'DEFAULT_AUTHENTICATION_CLASSES':['app01.MyAuth.MyAuthetication',],
'DEFAULT_PERMISSION_CLASSES':['app01.MyAuth.MyPermissions',],}

5.如何局部使用呢?

from app01.MyAuth import MyPermissionsclass BooksView(APIView):    permission_classes=[MyPermissions]    def get(self,request):
#request.user就是当前登录用户
print(request.user)
print(request.auth)
return Response('ok')

# 这样就是简单的局部使用了


drj框架之 访问频率类设置

    频率
频率是什么?
同一时间段内,只能访问多少次 频率的使用:
-写一个类:
from rest_framework.throttling import SimpleRateThrottle
class MyThrottling(SimpleRateThrottle):
scope='xxx'
#必须重写get_cache_key,返回什么,频率组件会以什么做限制(比如返回ip,它就以ip做限制,如果返回user_id,它会以用户id做限制)
def get_cache_key(self, request, view): return request.META.get('REMOTE_ADDR')

什么是频率,如何做设置

#drf给我们提供的频率控制类
from rest_framework.throttling import SimpleRateThrottle
class MyThrottling(SimpleRateThrottle):
scope='luffy'
def get_cache_key(self, request, view):
self.get_ident(request)
return request.META.get('REMOTE_ADDR')#注意: 这里的scope='luffy' 是按照项目名字来的,
# 认证的全局配置
REST_FRAMEWORK={
'DEFAULT_AUTHENTICATION_CLASSES':['app01.MyAuth.MyAuthetication',],
'DEFAULT_PERMISSION_CLASSES':['app01.MyAuth.MyPermissions',],
# 'DEFAULT_THROTTLE_CLASSES':['app01.MyAuth.MyThrottling'],
'DEFAULT_THROTTLE_RATES': {
'luffy': '10/day'
}
#注意 在做全局生效的时候 'luffy' : 必须和之前写的策略的scope = 'luffy' 要对应起来
#注意 '10/day' 这里可以自己定义, 一天访问10次,如果想要 一分钟访问6次 就是 '6/m'
}
from app01.MyAuth import MyPermissions
from app01.MyAuth import MyThrottling
class BooksView(APIView): throttle_classes=[MyThrottling]#局部使用

#注意: 在编写程序的时候访问频率还是很重要的,这样东西只要复制简单修改下就可以使用了!!!!

补充:
1 如果token信息放到请求头中,如何取?
# request._request.META
token=request.META.get('token')
2 django的GET,POST,META,body分别对应HTTP请求的哪部分?
-GET---->http请求路径中数据部分
-POST--->http请求以urlencode/formdata形式编码的body体部分
-META--->http请求头部信息
-body--->http请求请求体部分
-path,get_full_path(),FILES
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,489
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,290