首页 > 其他分享 >drf 1.版本组件

drf 1.版本组件

时间:2023-10-19 15:37:12浏览次数:37  
标签:None self request version 版本 kwargs 组件 ns drf

创建drf流程

配置文件

  • pip install django==3.2
  • django-admin startproject obj . 创建项目 加点会加到根目录
  • python manege.py startapp app01
  • pip install djangorestframework 按照drf组件

纯净版本django 设置
image

settings drf配置
REST_FRAMEWORK={}
查找这个REST_FRAMEWORK的方法流程

  1. from rest_framework.decorators import APIView
  2. APIView - api_settings - reload_api_settings 的方法
    UNAUTHENTICATED:None
    查找这个UNAUTHENTICATED的方法流程
  • APIView - dispach - initialize_request - Request - def user - self._authenticate() - self._not_authenticated()

版本

视图的使用
image
settings的设置
image

怎么去找到VERSION_PARAM

  • from rest_framework.versioning import QueryParameterVersioning
  • QueryParameterVersioning - BaseVersioning -VERSION_PARAM

基于GET参数-反向生成URL
执行流程

class BaseVersioning:
    default_version = api_settings.DEFAULT_VERSION
    allowed_versions = api_settings.ALLOWED_VERSIONS
    version_param = api_settings.VERSION_PARAM

    def determine_version(self, request, *args, **kwargs):
        msg = '{cls}.determine_version() must be implemented.'
        raise NotImplementedError(msg.format(
            cls=self.__class__.__name__
        ))

    def reverse(self, viewname, args=None, kwargs=None, request=None, format=None, **extra):
        return _reverse(viewname, args, kwargs, request, format, **extra)

    def is_allowed_version(self, version):
        if not self.allowed_versions:
            return True
        return ((version is not None and version == self.default_version) or
                (version in self.allowed_versions))
class QueryParameterVersioning(BaseVersioning):
    """
    GET /something/?version=0.1 HTTP/1.1
    Host: example.com
    Accept: 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
	#用来反向生成url
    def reverse(self, viewname, args=None, kwargs=None, request=None, format=None, **extra):
			#本质上是调用django上的reverse
        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
class APIView(View):
versioning_class = api_settings.DEFAULT_VERSIONING_CLASS #如果没有配置version versioning_class 走这里
    def determine_version(self, request, *args, **kwargs):
        """
        If versioning is being used, then determine any API version for the
        incoming request. Returns a two-tuple of (version, versioning_scheme)
        """
        if self.versioning_class is None:
            return (None, None)
        scheme = self.versioning_class()#这里指的是我views定义的那个类,类后面加()进行实例化。
        return (scheme.determine_version(request, *args, **kwargs), scheme)
    def initial(self, request, *args, **kwargs):
        """
        Runs anything that needs to occur prior to calling the method handler.
        """
        self.format_kwarg = self.get_format_suffix(**kwargs)

        # Perform content negotiation and store the accepted info on the request
        neg = self.perform_content_negotiation(request)
        request.accepted_renderer, request.accepted_media_type = neg

        # Determine the API version, if versioning is in use.
        version, scheme = self.determine_version(request, *args, **kwargs) # 1主要是先走这里
        request.version, request.versioning_scheme = version, scheme

        # Ensure that the incoming request is permitted
        self.perform_authentication(request)
        self.check_permissions(request)
        self.check_throttles(request)
class LoginView(APIView):
    versioning_class = QueryParameterVersioning

    def get(self, request):
        print(request.version)
        print(request.versioning_scheme)
        url = request.versioning_scheme.reverse("hh",request=request)
        print("反向生成url:", url)

        return Response("......")



单独对reverses分析


    def reverse(self, viewname, args=None, kwargs=None, request=None, format=None, **extra):
			#本质上是调用django上的reverse
        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
	def reverse(self, viewname, args=None, kwargs=None, request=None, format=None, **extra):
        return _reverse(viewname, args, kwargs, request, format, **extra)
	def _reverse(viewname, args=None, kwargs=None, request=None, format=None, **extra):
    """
    Same as `django.urls.reverse`, but optionally takes a request
    and returns a fully qualified URL, using the request to get the base URL.
    """
    if format is not None:
        kwargs = kwargs or {}
        kwargs['format'] = format
	def reverse(viewname, urlconf=None, args=None, kwargs=None, current_app=None):
    if urlconf is None:
        urlconf = get_urlconf()
    resolver = get_resolver(urlconf)
    args = args or []
    kwargs = kwargs or {}

    prefix = get_script_prefix()

    if not isinstance(viewname, str):
        view = viewname
    else:
        *path, view = viewname.split(':')

        if current_app:
            current_path = current_app.split(':')
            current_path.reverse()
        else:
            current_path = None

        resolved_path = []
        ns_pattern = ''
        ns_converters = {}
        for ns in path:
            current_ns = current_path.pop() if current_path else None
            # Lookup the name to see if it could be an app identifier.
            try:
                app_list = resolver.app_dict[ns]
                # Yes! Path part matches an app in the current Resolver.
                if current_ns and current_ns in app_list:
                    # If we are reversing for a particular app, use that
                    # namespace.
                    ns = current_ns
                elif ns not in app_list:
                    # The name isn't shared by one of the instances (i.e.,
                    # the default) so pick the first instance as the default.
                    ns = app_list[0]
            except KeyError:
                pass

            if ns != current_ns:
                current_path = None

            try:
                extra, resolver = resolver.namespace_dict[ns]
                resolved_path.append(ns)
                ns_pattern = ns_pattern + extra
                ns_converters.update(resolver.pattern.converters)
            except KeyError as key:
                if resolved_path:
                    raise NoReverseMatch(
                        "%s is not a registered namespace inside '%s'" %
                        (key, ':'.join(resolved_path))
                    )
                else:
                    raise NoReverseMatch("%s is not a registered namespace" % key)
        if ns_pattern:
            resolver = get_ns_resolver(ns_pattern, resolver, tuple(ns_converters.items()))

    return resolver._reverse_with_prefix(view, prefix, *args, **kwargs)


reverse_lazy = lazy(reverse, str)

    url = django_reverse(viewname, args=args, kwargs=kwargs, **extra)
    if request:
        return request.build_absolute_uri(url)
    return url

标签:None,self,request,version,版本,kwargs,组件,ns,drf
From: https://www.cnblogs.com/bxj123/p/17774804.html

相关文章

  • React学习笔记08- 组件的样式
    1.使用行内样式想给虚拟dom添加行内样式,需要使用表达式传入样式对象的方式来实现://注意这里的两个括号,第一个表示我们在要JSX里插入JS了,第二个是对象的括号<pstyle={{color:'red',fontSize:'14px'}}>Helloworld</p>行内样式需要写入一个样式对象,而这个样式对象的位置可......
  • React学习笔记06-函数式组件
    函数式组件即在React中通过函数的方式来声明一个组件importReactfrom"react"functionApp(){return(<div>函数式组件<div>hhh</div></div>)}/*16.8之前//无状态16.8之后reacthooks*/exportdef......
  • React学习笔记07-组件嵌套
    一个项目肯定是由多个组件构成的,组件之前必然会存在相互嵌套的关系在react中如果想要将一个组件嵌入到另外一个组件中只需要在父组件的render函数的返回值中放入子组件即可请看下面代码importReact,{Component}from"react"classNavbarextendsComponent{rende......
  • import { useRouter } from 'next/router'; 在非hooks 文件或组件中使用
    将 import{useRouter}from'next/router';改为 importRouterfrom"next/router";使用: Router.push('/');原来使用 import{useRouter}from'next/router';会导致报错如下  ......
  • React学习笔记05-类组件
    ES6的加入让JavaScript直接支持使用class来定义一个类,react创建组件的方式就是使用的类的继承,ES6class是目前官方推荐的使用方式,它使用了ES6标准语法来构建,看以下代码importReactfrom'react'importReactDOMfrom'react-dom'classAppextendsReact.Component{rend......
  • 解决多路由复用同一组件页面不刷新问题
     如果可以的话,这个才是最终解决方案,可以解决复用同一个vue页面导致的生命周期只在初始化调用,后续不调用的问题,监听路由的变化只是调用接口,不会触发生命周期......
  • kubernetes文档支持的版本
    https://kubernetes.io/zh-cn/docs/网站一共是可以阅读5个k8s版本的文档。这5个版本包括k8s最新版和最近的4个版本。例如当前最新版是1.28,那么文档网站可以阅读1.28、1.27、1.26、1.25、1.24这5个版本。在接下来的文档中,全部是以kubernetes1.28作为解释版本,文档里提到的需要实......
  • ZooKeeper集群版本升级
     ZooKeeper集群版本升级 环境描述:3节点的集群,当前版本为3.8.1,计划将所有节点版本升级到3.8.3。由于过半机制,即存活的节点数量>(非>=)所有节点数量的一半,则整个集群可以正常对外提供服务。举个例子,3个节点,最少存活2>(3/2=1.5)个节点,即允许有一个节点宕机下依旧能够对外服务。......
  • 38 异步组件
    异步,需要时才被加载<template><div><KeepAlive><component:is="tabComponent"></component></KeepAlive><button@click="change">切换组件</button></div></template>......
  • HarmonyOS/OpenHarmony原生应用-ArkTS万能卡片组件Slider
    滑动条组件,通常用于快速调节设置值,如音量调节、亮度调节等应用场景。该组件从APIVersion7开始支持。无子组件一、接口Slider(options?:{value?:number,min?:number,max?:number,step?:number,style?:SliderStyle,direction?:Axis,reverse?:boolean})从APIversion......