urls.py
url(r'^(?P<version>[v1|v2]+)/book$',VersionView.as_view()), # 127.0.0.1:8000/v1/book
settings.py中添加的
REST_FRAMEWORK = { "DEFAULT_VERSIONING_CLASS": "rest_framework.versioning.URLPathVersioning", "DEFAULT_VERSION": "v1", "ALLOWED_VERSIONS": ["v1", "v2"], "VERSION_PARAM": "version", # "DEFAULT_AUTHENTICATION_CLASSES": ["utils.Auth.MyAuth"] }
views.py
from django.shortcuts import render from rest_framework.views import APIView from rest_framework.response import Response # Create your views here. class VersionView(APIView): def get(self, request, version): print(request.version) # 版本 print(request.versioning_scheme) # 版本控制类的实例化对象 if request.version == "v1": return Response("v1版本的返回") elif request.version == "v2": return Response("v2版本的返回") return Response("版本不存在")