Django day28 频率组件,解析器
一:频率组件:
1、频率是什么?
节流,访问控制
2、
(1)内置的访问频率控制类SimpleRateThrottle
(2)写一个类,继承SimpleRateThrottle
class MyThrottle(SimpleRateThrottle): scope='aaa' def get_cache_key(self, request, view): return self.get_ident(request)
(3)在setting中:
REST_FRAMEWORK = { 'DEFAULT_THROTTLE_RATES':{ 'aaa':'10/m' } }
3、使用
局部使用: 在视图类中写 throttle_classes = [MyThrottle,] 全局使用: 在setting中写 'DEFAULT_THROTTLE_CLASSES':['app01.MyAuth.MyThrottle',], 局部禁用: 在视图类中写 throttle_classes = []
4、错误信息改成中文显示:
def throttled(self, request, wait): class MyThrottled(exceptions.Throttled): default_detail = '哈哈哈' extra_detail_singular = '还剩 {wait} 秒.' extra_detail_plural = '还剩 {wait} 秒' raise MyThrottled(wait)
二:解析器:
1、作用:传过来的数据,解析成字典
2、使用:
局部使用: from rest_framework.parsers import JSONParser,FormParser 在视图类中: parser_classes = [FormParser,] 全局使用 REST_FRAMEWORK = { 'DEFAULT_PARSER_CLASSES':[ 'rest_framework.parsers.JSONParser' ] }
3、局部使用指定的解析器:
在视图类中:
parser_classes = [FormParser,]