DRF版本組件

1.DRF版本組件配置類局部使用  django

  局部配置:api

    方式一:直接配置versioning_class=QueryParameterVersioningsession

    方式二:直接配置versioning_class=URLPathVersioningapp

    #也須要在settings.py中的REST_FRAMEWORK進行全局配置ide

    '''post

      default_version=api_settings.DEFAULT_VERSIONurl

      allowed_versions=api_settings.ALLOWED_VERSIONSspa

      version_param=api_settings.VERSION_PARAM3d

    '''版本控制

  

  settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'api.apps.ApiConfig',
    'rest_framework',
]
 
REST_FRAMEWORK = {
    #1版本組件
    'DEFAULT_VERSION':'v1',#默認的API版本
    'ALLOWED_VERSIONS':['v1','v2'],#容許的API版本
    'VERSION_PARAM':'version',#版本的key,默認的就是version(注意要和api接口中的key對應)
 
}
settings.py

   urls.py  

    drf主路由配置urls.py  

from django.conf.urls import url,include
urlpatterns = [
    url(r'^api/', include('api.urls')),
]
drf主路由配置urls.py:

    api主路由配置urls.py:

from django.conf.urls import url
from api import views
 
urlpatterns = [
    url(r'^(?P<version>v[\d+])/users/',views.UsersView.as_view(),name='users'),
]
 
api主路由配置urls.py:

  views.py:  

from django.shortcuts import render, HttpResponse
from rest_framework.views import APIView
from rest_framework.versioning import BaseVersioning, QueryParameterVersioning, URLPathVersioning
from django.urls import reverse
 
 
class UsersView(APIView):
    # API版本組件(局部配置)----返回結果都是同樣
    # 也須要在settings.py中的REST_FRAMEWORK進行全局配置
    '''
        default_version = api_settings.DEFAULT_VERSION
        allowed_versions = api_settings.ALLOWED_VERSIONS
        version_param = api_settings.VERSION_PARAM
    '''
    # (1)當版本號以查詢參數形式傳遞時:好比:http://127.0.0.1:8000/api/users/?version=v1
    # (推薦第2種方式,此方式url(r'^/users/',views.UsersView.as_view(),name='users'),)
    # versioning_class = QueryParameterVersioning
 
    # (2)當版本號以路徑urlpath形式傳遞時:好比:http://127.0.0.1:8000/api/v1/users/
    # (注意在urls.py中使用正則匹配url(r'^(?P<version>v[\d+])/users/',views.UsersView.as_view(),name='users'),)
    versioning_class = URLPathVersioning
 
    def get(self, request, *args, **kwargs):
        print(request.version)  # 獲取版本號
        print(request.versioning_scheme)  # 獲取到版本號處理的類對象,封裝了reverse方法
        print(request.versioning_scheme.reverse(viewname='users', request=request))  # 反向解析url
        # print(reverse(viewname='users',kwargs={'version':request.version}))#django原生解析,必須指定參數
 
        return HttpResponse('get_users')
 
    def post(self, request, *args, **kwargs):
        return HttpResponse('post_user')
views.py:

 

2.DRF版本組件配置類推薦全局配置  

  通常在版本組件中推薦使用URLPathVersioning類進行版本控制

  

  settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'api.apps.ApiConfig',
    'rest_framework',
]
 
REST_FRAMEWORK = {
    #版本組件(全局配置)----針對全部的繼承APIView的類,在認證+權限+節流以前
    'DEFAULT_VERSIONING_CLASS':'rest_framework.versioning.URLPathVersioning',#設置版本組件類---#http://127.0.0.1:8000/api/v1/users/
    'DEFAULT_VERSION':'v1',#默認的API版本
    'ALLOWED_VERSIONS':['v1','v2'],#容許的API版本
    'VERSION_PARAM':'version',#版本的key,默認的就是version(注意要和api接口中的key對應)
 
}
 
settings.py

 urls.py  

    drf主路由配置urls.py

from django.conf.urls import url,include
urlpatterns = [
    url(r'^api/', include('api.urls')),
]
drf主路由配置urls.py:

    api主路由配置urls.py:  

from django.conf.urls import url
from api import views
#http://127.0.0.1:8000/api/v1/users/
urlpatterns = [
    url(r'^(?P<version>v[\d+])/users/',views.UsersView.as_view(),name='users'),
]
api主路由配置urls.py:

  views.py:

from django.shortcuts import render, HttpResponse
from rest_framework.views import APIView
from django.urls import reverse
 
 
class UsersView(APIView):
    
    def get(self, request, *args, **kwargs):
        print(request.version)  # 獲取版本號
        print(request.versioning_scheme)  # 獲取到版本號處理的類對象,封裝了reverse方法
        print(request.versioning_scheme.reverse(viewname='users', request=request))  # 反向解析url
        # print(reverse(viewname='users',kwargs={'version':request.version}))#django原生解析,必須指定參數
 
        return HttpResponse('get_users')
 
    def post(self, request, *args, **kwargs):
        return HttpResponse('post_user')
 
views.py:
相關文章
相關標籤/搜索