Django-Rest frameworw之版本

  • 时间:
  • 来源:互联网
  • 文章标签:

一、示例展示

=============******=============

            1、基于url的get传参方式 #如:/users?version=v1

            2、基于url的正则方式 # /v1/users/

            3、基于 accept 请求头方式 如:Accept: application/json; version=1.0

            4. 基于主机名方法  如:v1.example.com

            5、基于django路由系统的namespace    如:example.com/v1/users/

=============******=============

全局使用:

REST_FRAMEWORK = {
    'DEFAULT_VERSIONING_CLASS':"rest_framework.versioning.URLPathVersioning",
    'DEFAULT_VERSION': 'v1',
    'ALLOWED_VERSIONS': ['v1', 'v2'],
    'VERSION_PARAM': 'version' 
}

1、基于url的get传参方式 #如:/users?version=v1

in settings.py
	REST_FRAMEWORK = {
	    'DEFAULT_VERSION': 'v1',            # 默认版本
	    'ALLOWED_VERSIONS': ['v1', 'v2'],   # 允许的版本
	    'VERSION_PARAM': 'version'          # URL中获取值的key
	}

in urls.py
	from django.conf.urls import url, include
	from web.views import TestView

	urlpatterns = [
	    url(r'^test/', TestView.as_view(),name='test'),
	]
in views.py
	#!/usr/bin/env python
	# -*- coding:utf-8 -*-
	from rest_framework.views import APIView
	from rest_framework.response import Response
	from rest_framework.versioning import QueryParameterVersioning


	class TestView(APIView):
	    versioning_class = QueryParameterVersioning

	    def get(self, request, *args, **kwargs):

	        # 获取版本
	        print(request.version)
	        # 获取版本管理的类
	        print(request.versioning_scheme)

	        # 反向生成URL
	        reverse_url = request.versioning_scheme.reverse('test', request=request)
	        print(reverse_url)

	        return Response('GET请求,响应内容')

	    def post(self, request, *args, **kwargs):
	        return Response('POST请求,响应内容')

	    def put(self, request, *args, **kwargs):
	        return Response('PUT请求,响应内容')

2、基于url的正则方式 # /v1/users/

in settings.py
	REST_FRAMEWORK = {
	    'DEFAULT_VERSION': 'v1',            # 默认版本
	    'ALLOWED_VERSIONS': ['v1', 'v2'],   # 允许的版本
	    'VERSION_PARAM': 'version'          # URL中获取值的key
	}



in urls.py
	from django.conf.urls import url, include
	from web.views import TestView

	urlpatterns = [
	    url(r'^(?P<version>[v1|v2]+)/test/', TestView.as_view(), name='test'),
	]
in views.py
	#!/usr/bin/env python
	# -*- coding:utf-8 -*-
	from rest_framework.views import APIView
	from rest_framework.response import Response
	from rest_framework.versioning import URLPathVersioning


	class TestView(APIView):
	    versioning_class = URLPathVersioning

	    def get(self, request, *args, **kwargs):
	        # 获取版本
	        print(request.version)
	        # 获取版本管理的类
	        print(request.versioning_scheme)

	        # 反向生成URL
	        reverse_url = request.versioning_scheme.reverse('test', request=request)
	        print(reverse_url)

	        return Response('GET请求,响应内容')

	    def post(self, request, *args, **kwargs):
	        return Response('POST请求,响应内容')

	    def put(self, request, *args, **kwargs):
	        return Response('PUT请求,响应内容')

3、基于 accept 请求头方式 如:Accept: application/json; version=1.0

in settings.py
	REST_FRAMEWORK = {
	    'DEFAULT_VERSION': 'v1',            # 默认版本
	    'ALLOWED_VERSIONS': ['v1', 'v2'],   # 允许的版本
	    'VERSION_PARAM': 'version'          # URL中获取值的key
	}
in urls.py
	from django.conf.urls import url, include
	from web.views import TestView

	urlpatterns = [
	    url(r'^test/', TestView.as_view(), name='test'),
	]
in views.py
	#!/usr/bin/env python
	# -*- coding:utf-8 -*-
	from rest_framework.views import APIView
	from rest_framework.response import Response
	from rest_framework.versioning import AcceptHeaderVersioning


	class TestView(APIView):
	    versioning_class = AcceptHeaderVersioning

	    def get(self, request, *args, **kwargs):
	        # 获取版本 HTTP_ACCEPT头
	        print(request.version)
	        # 获取版本管理的类
	        print(request.versioning_scheme)
	        # 反向生成URL
	        reverse_url = request.versioning_scheme.reverse('test', request=request)
	        print(reverse_url)

	        return Response('GET请求,响应内容')

	    def post(self, request, *args, **kwargs):
	        return Response('POST请求,响应内容')

	    def put(self, request, *args, **kwargs):
	        return Response('PUT请求,响应内容')


4. 基于主机名方法  如:v1.example.com

in settings.py
	ALLOWED_HOSTS = ['*']
	REST_FRAMEWORK = {
	    'DEFAULT_VERSION': 'v1',  # 默认版本
	    'ALLOWED_VERSIONS': ['v1', 'v2'],  # 允许的版本
	    'VERSION_PARAM': 'version'  # URL中获取值的key
	}
in urls.py
	from django.conf.urls import url, include
	from web.views import TestView

	urlpatterns = [
	    url(r'^test/', TestView.as_view(), name='test'),
	]
in views.py
	#!/usr/bin/env python
	# -*- coding:utf-8 -*-
	from rest_framework.views import APIView
	from rest_framework.response import Response
	from rest_framework.versioning import HostNameVersioning


	class TestView(APIView):
	    versioning_class = HostNameVersioning

	    def get(self, request, *args, **kwargs):
	        # 获取版本
	        print(request.version)
	        # 获取版本管理的类
	        print(request.versioning_scheme)
	        # 反向生成URL
	        reverse_url = request.versioning_scheme.reverse('test', request=request)
	        print(reverse_url)

	        return Response('GET请求,响应内容')

	    def post(self, request, *args, **kwargs):
	        return Response('POST请求,响应内容')

	    def put(self, request, *args, **kwargs):
	        return Response('PUT请求,响应内容')

5、基于django路由系统的namespace    如:example.com/v1/users/

in settings.py
	REST_FRAMEWORK = {
	    'DEFAULT_VERSION': 'v1',  # 默认版本
	    'ALLOWED_VERSIONS': ['v1', 'v2'],  # 允许的版本
	    'VERSION_PARAM': 'version'  # URL中获取值的key
	}
in urls.py
	from django.conf.urls import url, include
	from web.views import TestView

	urlpatterns = [
	    url(r'^v1/', ([url(r'test/', TestView.as_view(), name='test'),], None, 'v1')),

	    url(r'^v2/', ([url(r'test/', TestView.as_view(), name='test'),], None, 'v2')),
	]
in views.py
	#!/usr/bin/env python
	# -*- coding:utf-8 -*-
	from rest_framework.views import APIView
	from rest_framework.response import Response
	from rest_framework.versioning import NamespaceVersioning


	class TestView(APIView):
	    versioning_class = NamespaceVersioning

	    def get(self, request, *args, **kwargs):
	        # 获取版本
	        print(request.version)
	        # 获取版本管理的类
	        print(request.versioning_scheme)
	        # 反向生成URL
	        reverse_url = request.versioning_scheme.reverse('test', request=request)
	        print(reverse_url)

	        return Response('GET请求,响应内容')

	    def post(self, request, *args, **kwargs):
	        return Response('POST请求,响应内容')

	    def put(self, request, *args, **kwargs):
	        return Response('PUT请求,响应内容')


本文链接http://www.taodudu.cc/news/show-647376.html