rest framework的框架實現
阿新 • • 發佈:2019-05-08
default n) from 127.0.0.1 *args query obj https sch
一版本
版本實現根據訪問的的方式有以下幾種
a : https://127.0.0.1:8000/users?version=v1
#settings.py
REST_FRAMEWORK = { ‘DEFAULT_VERSION‘: ‘v1‘, # 默認版本 ‘ALLOWED_VERSIONS‘: [‘v1‘, ‘v2‘], # 允許的版本 ‘VERSION_PARAM‘: ‘version‘ # URL中獲取值的key }
#urls.py
urlpatterns = [ url(r‘^users/‘, views.UserView.as_view(),name=‘xxx‘), ]
# views.py
from django.shortcuts import render, HttpResponse
from rest_framework.views import APIView
from rest_framework.versioning import QueryParameterVersioning
from django import forms
from django.urls import reverse
class UserView(APIView):
versioning_class = QueryParameterVersioning
def get(self, request, *args, **kwargs):
print(request.version)
print(request.versioning_scheme) #<rest_framework.versioning.URLPathVersioning object at 0x000001330C3135F8>
u1=request.versioning_scheme.reverse(viewname=‘xxx‘,request=request)
print(u1) #http://127.0.0.1:8000/api/users/?version=v1
return HttpResponse(‘用戶列表‘)
rest framework的框架實現