1、路由
a、一级路由
from django.contrib import admin from django.urls import path, include from api import urls as api_url urlpatterns = [ path('admin/', admin.site.urls), # version 设置版本控制 path('api/<version>/', include(api_url)), ]
b、二级路由(分发)
from django.urls import path, include from api.views import course # # {'get': 'list', 'post': 'create'} # {'get': 'retrieve', 'put': 'update', 'delete': 'destroy'} # urlpatterns = [ path('course/', course.CourseVies.as_view({'get': 'list'})), path('course/<pk>/', course.CourseVies.as_view({'get': 'retrieve'})), ]
2、全局配置
REST_FRAMEWORK = { # 控制版本 'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.URLPathVersioning', 'ALLOWED_VERSIONS': ['v1', 'v2'], # 允许的版本 'VERSION_PARAM': 'version', # 参数 'DEFAULT_VERSION': 'v1', # 默认版本 }