framework —— 版本
1.原生版本django模拟
from django.shortcuts import render from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.authentication import BaseAuthentication from rest_framework.versioning import QueryParameterVersioning,URLPathVersioning # Create your views here. class UserView(APIView): #基于url传参 # versioning_class = QueryParameterVersioning # 基于URL http://127.0.0.1:8001/api/v2/users/ versioning_class = URLPathVersioning # 基于子域名 http://v1.luffy.com/users/ # versioning_class = HostNameVersioning def get(self,request,*args,**kwargs): self.dispatch print(request.version)#QueryParameterVersioning().datemin_version() print(request.versioning_scheme) #QueryParameterVersioning() # 当前版本一样的URL url = request.versioning_scheme.reverse(viewname=‘u‘,request=request) print(url) # 当前版本不一样的URL # from django.urls import reverse # url = reverse(viewname=‘u‘,kwargs={‘version‘:‘v2‘}) # print(url) return Response(‘...‘) #如果v1 # version = request.query_params.get(‘version‘) # if request.version == ‘v1‘: # ret = { # ‘code‘:111, # ‘msg‘:‘版本v1的内容‘ # } # #如果v2 # elif request.version == ‘v2‘: # ret = { # ‘code‘:111, # ‘msg‘:‘版本v2的内容‘ # } # else: # ret = { # ‘code‘:0, # ‘msg‘:‘不支持版本‘ # } # return Response(ret)
2.framework里的版本类