一 .版本设置(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项目下urlsfrom 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下urlsfrom django.conf.urls import url,include from myapp.viwes import show urlpatterns = [ url(r‘^home/$‘, show.HomeViwes.as_view()), ]
原文地址:https://www.cnblogs.com/lovershowtime/p/11681842.html
时间: 2024-10-08 03:24:15