码迷,mamicode.com
首页 > 其他好文 > 详细

rest_framework-版本-总结完结篇

时间:2018-07-30 14:53:21      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:scheme   self   对象   views   settings   api   获取   版本   默认   

总urls.py

from django.conf.urls import url, include
urlpatterns = [
    url(r^api/, include(api.urls))
]

 

分流后urls.py

from django.conf.urls import url
from api import views
urlpatterns = [
    url(r^(?P<version>[v1|v2]+)/user/$, views.UserView.as_view(),name=us)
]

 

settings.py

REST_FRAMEWORK = {
    "DEFAULT_VERSIONING_CLASS":"rest_framework.versioning.URLPathVersioning",
    "DEFAULt_VERSION":v1,  #默认为1
    "ALLOWED_VERSIONS":[v1,v2], #仅允许v1或者v2访问
    "VERSION_PARAM":version,

}

views.py 视图

from django.shortcuts import render, HttpResponse
from rest_framework.views import APIView
from rest_framework.versioning import QueryParameterVersioning,URLPathVersioning
from django.urls import reverse



# Create your views here.

class UserView(APIView):
    def get(self, request, *args, **kwargs):
        #获取版本
        print(request.version)
        #获取处理版本的对象
        print(request.versioning_scheme)

        #反向生成URL(rest_framework)
        u1 = request.versioning_scheme.reverse(viewname=us,request=request)
        print(u1)

        u2 = reverse(viewname=uuu, kwargs={version:1212})
        print(u2)
        return HttpResponse(ok)

 

rest_framework-版本-总结完结篇

标签:scheme   self   对象   views   settings   api   获取   版本   默认   

原文地址:https://www.cnblogs.com/Liang-jc/p/9390116.html

(1)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!