程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Note: Django rest Framework version use

編輯:Python

I can't use this for the time being restframework The version inside version, So we write classes that directly inherit the classes that others have written , as follows :

1, views.py View

from django.shortcuts import render
from django.http import HttpResponse
from rest_framework.views import APIView
from rest_framework.versioning import BaseVersioning,URLPathVersioning # Introduce here 
class VersionView(APIView):
versioning_class = URLPathVersioning # Use here , Note that this is not a list of classes , It's just one. ,.
def get(self,request, *args, **kwargs):
print(request.version) # Get version 
print(request.versioning_scheme) # Get version of object 
url = request.versioning_scheme.reverse(viewname='xxx', request=request) # Used for reverse generation url
print(url) # According to the preceding url.py In is the setting , Here is the generation of : http://127.0.0.1:8000/api/v1/
return HttpResponse('hello world')

2, urls.py Below :

from django.contrib import admin
from django.urls import re_path, path
from django.conf.urls import url, include
from api import views
urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'^api/(?P<version>[v1|v2]+)/$', views.VersionView.as_view(), name='xxx')
]

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved