• 十二.Django---framework框架 版本设置


    一 .版本设置(url上版本迭代)

    https://www.jb51.net/article/166249.htm     版本控制

    https://www.cnblogs.com/liqianglog/p/11133615.html    版本使用反向

    https://www.cnblogs.com/welan/p/10141126.html   版本控制流程

    1.局部版本(作用于单个视图)

    settings 

    REST_FRAMEWORK = {
             'DEFAULT_RENDERER_CLASSES':['rest_framework.renderers.JSONRenderer',
    'rest_framework.renderers.BrowsableAPIRenderer',
    ],

    'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.QueryParameterVersioning',
    'ALLOWED_VERSIONS': ['v1', 'v2','v3','v4'], # 允许的版本
    'VERSION_PARAM': 'version', # 参数
    'DEFAULT_VERSION': 'v1', # 默认版本

    }


    REST_FRAMEWORK = { 'DEFAULT_RENDERER_CLASSES':['rest_framework.renderers.JSONRenderer', 'rest_framework.renderers.BrowsableAPIRenderer', ], 'DEFAULT_VERSIONING_CLASS': '', 'ALLOWED_VERSIONS': ['v1', 'v2','v3','v4'], # 允许的版本 'VERSION_PARAM': 'version', # 参数 'DEFAULT_VERSION': 'v1', # 默认版本 }
    INSTALLED_APPS = [

    'rest_framework',
    ]
     
    urls下  settings

    from
    django.conf.urls import url,include from django.contrib import admin urlpatterns = [ url('admin/', admin.site.urls), url(r'^api/', include('myapp.urls')), # url(r'^api/(?P<version>w+)/', include('myapp.urls')), ]
    urls 项目myapp下
    from
    django.conf.urls import url,include from myapp.viwes import show urlpatterns = [ url(r'^home/$', show.HomeViwes.as_view()), # url(r'^home/(?P<version>[v1|v2])$', show.HomeViwes.as_view()), ]
    viwes

    from
    django.shortcuts import render,HttpResponse from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.versioning import QueryParameterVersioning class HomeViwes(APIView): versioning_class = QueryParameterVersioning # 局部配置请求参数处理 版本 def get(self,request,*args,**kwargs): print(request.version,"这是版本啊哈哈哈哈哈") return Response('哈哈哈哈哈哈1111111111') # return HttpResponse("111111111111111111")

    2.全局部版本(推荐作用于每个视图)

    settings

    STATIC_URL = '/static/' REST_FRAMEWORK = { 'DEFAULT_RENDERER_CLASSES':['rest_framework.renderers.JSONRenderer', 'rest_framework.renderers.BrowsableAPIRenderer', ], 'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.URLPathVersioning', 'ALLOWED_VERSIONS': ['v1', 'v2','v3'], # 允许的版本 'VERSION_PARAM': 'version', # 参数 'DEFAULT_VERSION': 'v1', # 默认版本 }
    settings 下urls

    from
    django.conf.urls import url,include from django.contrib import admin urlpatterns = [ url('admin/', admin.site.urls), url(r'^api/', include('myapp.urls')), # url(r'^api/(?P<version>w+)/', include('myapp.urls')), ]
    myapp项目下urls
    from
    django.conf.urls import url,include from myapp.viwes import show urlpatterns = [ # url(r'^home/$', show.HomeViwes.as_view()), url(r'^(?P<version>w+)/home$', show.HomeViwes.as_view()), # url(r'^(?P<version>[v1|v2]+)/home$', show.HomeViwes.as_view()), ]
    viwes

    from
    django.shortcuts import render,HttpResponse from rest_framework.views import APIView from rest_framework.response import Response class HomeViwes(APIView): def get(self,request,*args,**kwargs): print(request.version,"这是版本啊哈哈哈哈哈") return Response('哈哈哈哈哈哈1111111111') # return HttpResponse("111111111111111111")

    2.全局配置加在分发之前

    setting 下urls

    from
    django.conf.urls import url,include from django.contrib import admin urlpatterns = [ url('admin/', admin.site.urls), url(r'^api/(?P<version>w+)/', include('myapp.urls')), ]
    myapp下urls
    from
    django.conf.urls import url,include from myapp.viwes import show urlpatterns = [ url(r'^home/$', show.HomeViwes.as_view()), ]
  • 相关阅读:
    「JOISC 2020 Day3」收获
    $ ext{Min25}$筛
    [做题记录-图论] [NEERC2017]Journey from Petersburg to Moscow [关于处理路径前$k$大的一种方法]
    [复习笔记]一些有意思的解法技巧 (转 Dpair
    [比赛记录] CSP2021-S 题解
    [转]C++学习心得
    Sigmoid function in NN
    Kernel Regression from Nando's Deep Learning lecture 5
    Python codes
    php中mail()改用msmtp发送邮件
  • 原文地址:https://www.cnblogs.com/lovershowtime/p/11681842.html
Copyright © 2020-2023  润新知