路由器

作者: Lonelyroots | 来源:发表于2022-06-20 20:02 被阅读0次

    路由器

    apps/school/urls.py:

    # (四、短信验证码)
    
    """路由器"""
    # 只有当视图函数里的StudentViewSet继承于ViewSet及其ViewSet的子类才可以使用路由器.
    from rest_framework.routers import SimpleRouter
    from .views import StudentViewSet,ClassesViewSet
    
    urlpatterns = [
    
    ]
    
    router = SimpleRouter()      # 创建路由器
    router.register('students',StudentViewSet)      # 注册学生的路由
    router.register('classes',ClassesViewSet)      # 注册班级的路由
    urlpatterns += router.urls      # 拼接获取生成的路由列表
    

    6. 限流

    限流的配置,可以直接从官网上爬取源码
    https://www.django-rest-framework.org/api-guide/throttling/

    # (四、短信验证码:限流、接口文档)
    REST_FRAMEWORK = {
        'DEFAULT_THROTTLE_CLASSES': [
            'rest_framework.throttling.AnonRateThrottle',       # 没有登录的匿名用户
            'rest_framework.throttling.UserRateThrottle'        # 登录的用户
        ],
        'DEFAULT_THROTTLE_RATES': {
            'anon': '100/day',      # 访问频次,单位可以是hour、minute、second
            'user': '1000/day'      # 访问频次
        },
        'DEFAULT_SCHEMA_CLASS':'rest_framework.schemas.coreapi.AutoSchema'
    }
    

    生成官方的接口文档

    下载依赖

    先换源安装模块
    pip install coreapi -i https://pypi.douban.com/simple

    在settings里进行配置

    # (四、短信验证码:限流、接口文档)
    REST_FRAMEWORK = {
        'DEFAULT_THROTTLE_CLASSES': [
            'rest_framework.throttling.AnonRateThrottle',       # 没有登录的匿名用户
            'rest_framework.throttling.UserRateThrottle'        # 登录的用户
        ],
        'DEFAULT_THROTTLE_RATES': {
            'anon': '100/day',      # 访问频次,单位可以是hour、minute、second
            'user': '1000/day'      # 访问频次
        },
        'DEFAULT_SCHEMA_CLASS':'rest_framework.schemas.coreapi.AutoSchema'
    }
    

    在主路由界面配置接口文档路由

    Ls_shopping_center/urls.py:

    from django.contrib import admin
    from django.urls import path,include
    from rest_framework.documentation import include_docs_urls
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('school/',include('school.urls')),
        path('docs/',include_docs_urls('lsu Ls_shopping_center API 接口文档')),
    ]
    
    运行结果

    文章到这里就结束了!希望大家能多多支持Python(系列)!六个月带大家学会Python,私聊我,可以问关于本文章的问题!以后每天都会发布新的文章,喜欢的点点关注!一个陪伴你学习Python的新青年!不管多忙都会更新下去,一起加油!

    Editor:Lonelyroots

    相关文章

      网友评论

          本文标题:路由器

          本文链接:https://www.haomeiwen.com/subject/cbwjvrtx.html