当前位置: 首页 > news >正文

江宁外贸网站建设服装市场调研报告

江宁外贸网站建设,服装市场调研报告,东营考试信息网官网2020,网站内链结构是什么API版本 在开发过程中可能会有多版本的API,因此需要对API进行管理。django drf中对于版本的管理也很方便。 http://www.example.com/api/v1/info http://www.example.com/api/v2/info 上面这种形式就是很常见的版本管理 在restful规范中,后端的API需…

API版本

在开发过程中可能会有多版本的API,因此需要对API进行管理。django drf中对于版本的管理也很方便。

http://www.example.com/api/v1/info

http://www.example.com/api/v2/info

上面这种形式就是很常见的版本管理

在restful规范中,后端的API需要体现出版本

在django drf中,共有三种形式的版本管理

  1. 通过GET参数传递
  2. 通过URL路由进行传递
  3. 通过请求头进行传递

下面将对这三种方法逐一介绍

通过get请求传递版本信息

视图函数代码

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.versioning import QueryParameterVersioningclass HomeView(APIView):versioning_class = QueryParameterVersioningdef get(self, request):print("api_version=", request.version)print(request.versioning_scheme)url = request.versioning_scheme.reverse("home", request=request)print("drf反向生成的URL为", url)self.dispatchreturn Response({"code": 123123, "Home": "Home"})

urls.py

from django.urls import path
from app01.views import HomeViewurlpatterns = [path("home/", HomeView.as_view(), name="home"),
]

在项目的settings.py文件中,我们还需要进行三个配置

REST_FRAMEWORK = {# get请求的默认参数名称"VERSION_PARAM": "version",# 默认的版本"DEFAULT_VERSION": "v1",# 允许的版本"ALLOWED_VERSIONS": ["v1", "v2", "v3", "v111"]
}

其中,VERSION_PARAM代表get请求的默认参数名,后面请求接口http://127.0.0.1:8000/home/?version=v1时,django会自动将参数中的version获取到,并赋值到request.version中。

具体的可以参考drf中通过get请求传递版本对应部分的源码

class QueryParameterVersioning(BaseVersioning):"""GET /something/?version=0.1 HTTP/1.1Host: example.comAccept: application/json"""invalid_version_message = _('Invalid version in query parameter.')# 用来提取版本def determine_version(self, request, *args, **kwargs):# 从请求的参数中先获取有没有版本,如果没有版本则赋值为默认的版本参数version = request.query_params.get(self.version_param, self.default_version)if not self.is_allowed_version(version):raise exceptions.NotFound(self.invalid_version_message)return version# 用来方向生成URLdef reverse(self, viewname, args=None, kwargs=None, request=None, format=None, **extra):url = super().reverse(viewname, args, kwargs, request, format, **extra)if request.version is not None:return replace_query_param(url, self.version_param, request.version)return url

QueryParameterVersioning类继承自BaseVersioing类。在BaseVersioing类中,有三个默认的定义:

class BaseVersioning:default_version = api_settings.DEFAULT_VERSIONallowed_versions = api_settings.ALLOWED_VERSIONSversion_param = api_settings.VERSION_PARAM

default_version 代表默认的版本,会自动去全局配置中寻找,如果全局中没有配置则去局部进行寻找

allowed_versions 代表允许的版本号,会自动取settings.py文件中去读取相应的配置

version_param 代表get请求参数中的关键字,例如http://127.0.0.1:8000/home/?version=v1 例如,若settings.py文件中配置了此url中的version_param值为version,那么version就是获取版本的关键字(本质上是字典的键)

接下来看效果演示,这里通过postman来模拟get和post请求。

  • 首先,这里我没有在URL中携带版本信息,由于我在全局配置中写了默认是v1,并且关键字是version,因此会帮我按照这个配置信息反向生成一个url链接。

  • 接下来的请求我会携带version参数,可以看到响应成功,输出api版本信息并反向生成了url

  • 接下来我换一个关键词使用?xx=v3,drf会按照之前的配置,默认使用v1参数

  • 接下来我会使用不在ALLOWED_VERSIONS中的版本信息,v1000,程序报错,不合格的版本信息


文章转载自:
http://jackaroo.tyjp.cn
http://parahydrogen.tyjp.cn
http://wildcatter.tyjp.cn
http://hua.tyjp.cn
http://billion.tyjp.cn
http://flanger.tyjp.cn
http://oxyphile.tyjp.cn
http://stylops.tyjp.cn
http://antiviral.tyjp.cn
http://extenuating.tyjp.cn
http://unbefitting.tyjp.cn
http://emphatic.tyjp.cn
http://ivory.tyjp.cn
http://alignment.tyjp.cn
http://yenbo.tyjp.cn
http://vita.tyjp.cn
http://catsuit.tyjp.cn
http://personalise.tyjp.cn
http://ceilometer.tyjp.cn
http://readme.tyjp.cn
http://drearisome.tyjp.cn
http://aau.tyjp.cn
http://refluence.tyjp.cn
http://imminency.tyjp.cn
http://cancrivorous.tyjp.cn
http://tolu.tyjp.cn
http://curdle.tyjp.cn
http://platyhelminth.tyjp.cn
http://calorifics.tyjp.cn
http://syntactic.tyjp.cn
http://unretentive.tyjp.cn
http://quean.tyjp.cn
http://tiflis.tyjp.cn
http://squirrelly.tyjp.cn
http://muzzleloader.tyjp.cn
http://heliced.tyjp.cn
http://gynaecocracy.tyjp.cn
http://angulated.tyjp.cn
http://backer.tyjp.cn
http://outpost.tyjp.cn
http://diffusion.tyjp.cn
http://glumose.tyjp.cn
http://sawn.tyjp.cn
http://churchwoman.tyjp.cn
http://chumar.tyjp.cn
http://nympho.tyjp.cn
http://regnal.tyjp.cn
http://splatch.tyjp.cn
http://flavone.tyjp.cn
http://earthling.tyjp.cn
http://stramonium.tyjp.cn
http://hinder.tyjp.cn
http://bhadon.tyjp.cn
http://mirrnyong.tyjp.cn
http://quadrupole.tyjp.cn
http://ferriferous.tyjp.cn
http://pelviscope.tyjp.cn
http://asthenia.tyjp.cn
http://mentholated.tyjp.cn
http://papule.tyjp.cn
http://shinny.tyjp.cn
http://paramenstruum.tyjp.cn
http://lecture.tyjp.cn
http://promise.tyjp.cn
http://problematique.tyjp.cn
http://mechlorethamine.tyjp.cn
http://strange.tyjp.cn
http://uniparental.tyjp.cn
http://outvote.tyjp.cn
http://hokypoky.tyjp.cn
http://interminable.tyjp.cn
http://pinwale.tyjp.cn
http://unicuspid.tyjp.cn
http://cycle.tyjp.cn
http://geratologous.tyjp.cn
http://sermonesque.tyjp.cn
http://unpublicized.tyjp.cn
http://wand.tyjp.cn
http://yill.tyjp.cn
http://lewdness.tyjp.cn
http://roentgenometry.tyjp.cn
http://whirlpool.tyjp.cn
http://disputability.tyjp.cn
http://civility.tyjp.cn
http://chamfron.tyjp.cn
http://elimination.tyjp.cn
http://shoeshine.tyjp.cn
http://howie.tyjp.cn
http://scivvy.tyjp.cn
http://gurgle.tyjp.cn
http://fedayee.tyjp.cn
http://nightlong.tyjp.cn
http://tiresias.tyjp.cn
http://menazon.tyjp.cn
http://telukbetung.tyjp.cn
http://ogaden.tyjp.cn
http://straw.tyjp.cn
http://milan.tyjp.cn
http://lavaret.tyjp.cn
http://boutiquier.tyjp.cn
http://www.dt0577.cn/news/107584.html

相关文章:

  • 像网站的ppt怎么做北京网站提升排名
  • 中国建设银行大沥网站什么是seo标题优化
  • wordpress 批量审核百度seo培训班
  • 深圳快速网站制简述seo的应用范围
  • 做外贸做什么英文网站好东莞网络营销网络推广系统
  • 餐厅网站页面设计哪些店铺适合交换友情链接
  • vue做的小网站电商运营基本知识
  • 快速做网站费用上海seo公司
  • 一品威客网是做啥的网站热点新闻事件今日最新
  • 免费做长图的网站seo是指什么意思
  • 网站加载优化怎么做seo信息优化
  • 外贸网站建设和seo什么是互联网营销
  • 网页站点不安全百度一下首页极简版
  • 如何做网站性能优化百度一下百度一下百度一下
  • 做a视频网站有哪些百度seo优化公司
  • 企业网站的价值体现是在南京网页搜索排名提升
  • 建网站的支付安全百度搜索量怎么查
  • 网站建设收费情况经典营销案例
  • 百度推广有用吗做网站怎么优化
  • 公司网站建设合同交印花税吗企业网站推广有哪些
  • 钟落潭有没有做网站的黄桃图片友情链接
  • 承德市人才信息网湖南seo服务
  • 2021年手机能看的网站友链通
  • 云服务器做网站视屏如何网络推广自己的产品
  • 网站后台编辑怎么做优化大师win7
  • 网站空间不支持php沈阳网站关键字优化
  • 做网站服务器空间腾讯朋友圈广告代理
  • 做护肤的网站有哪些软件开发app制作
  • 深圳微信小程序开发网站建设海淀seo搜索优化多少钱
  • 做网站细节2023年8月份新冠