赞
踩
配置接口文档,方便我们后期接口写了很多的时候,自己查看,同时,也可以给前端等人员查看我们的接口,进行使用。
步骤:
import coreapi #不导入包会报posixoath的错误
REST_FRAMEWORK = {
#接口文档生成配置:以网页的形式进行展式,所以还要再主路由配置上添加一个路由,用来访问
'DEFAULT_SCHEMA_CLASS':'rest_framework.schemas.coreapi.AutoSchema',
'DEFAULT_AUTHENTICATION_CLASSES':(
'rest_framework_jwt.authentication.JSONWebTokenAuthentication', #token认证类
'rest_framework.authentication.SessionAuthentication',#session认证类
'rest_framework.authentication.BasicAuthentication',#认证基类
),
}
from django.contrib import admin
from django.urls import path,include
from rest_framewoek.documentation import include_docs_urls
urlpatterns = [
path('admin/',admin.site.urls),
path('docs/'include_docs_urls('接口文档')),#配置接口文档路由,文档标题可修改
]
class SNViewSet(viewsets.ModelViewSet):
"""
list:返回所有的数据
"""
queryset = SNData.objects.filter(is_delete = false)
serializer_class = SNDataSerializer
配置限流,可以保护服务器,防止服务器超出所能承载的压力,也能防止恶意攻击。
在settings.py文件中添加如下配置信息:
'DEFAULT_THROTTLE_CLASSES':[
'rest_framework.throttling.AnonRateThrottle',
'rest_framework.throttling.UserRateThrottle'
],
'DEFAULT_THROTTLE_RATES':[
'anon':'3/second', #不登陆的用户每秒钟可调用接口3次,可自行修改 minute分钟,hour小时,day天
'user':'10/second' #登录的用户每秒钟可调用接口10次
]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。