zoukankan      html  css  js  c++  java
  • 9.21

    drf post请求错误

    在s7129查看认证的时候

    写了一会,发现是post,然后不用浏览器访问了,用postman访问(post好啊,只能是你)

    然后发现一直异常, 明明走了try,怎么还走except呢!

    然后把程序复制了一份,发现还是报错,说明不是权限和节流等的影响我的认证.

    把认证的过程看了几遍,不是很懂,终于有个图写的好点
    1568017683422
    是走得return()元祖,然后自己自定义的类中有方法, auth....的第一个方法,返回的()

    但是怎么走的全局的我不懂,因为他是settings配置的, 配置的有在api.settings中调用了.不过好歹他能用了,就是在写了一个类,全局配置或者局部配置了之后,就像中间件一样,装饰器一样,先走这个,认证完之后再去别的功能.

    但是和我的逻辑还是不一样,我的只是一个简单的post方法,as_view之后直接走的, 又没有别的什么方法,可以通过dispatch,直接走反射的,却不行.

    最后打印代码才发现,request为空,(解决之道,打印) 有request的post还要传值,传了值却还不行. 原来是用Body传值,而不是用params传值,之前就传错过.

    1569051073802

    settings和apisettings的关系

    自己的settings.(一点端倪)

    """
    Settings for REST framework are all namespaced in the REST_FRAMEWORK setting.
    For example your project's `settings.py` file might look like this:
    
    REST_FRAMEWORK = {
        'DEFAULT_RENDERER_CLASSES': [
            'rest_framework.renderers.JSONRenderer',
            'rest_framework.renderers.TemplateHTMLRenderer',
        ],
    

    全局变量

        def _authenticate(self):
            """
            Attempt to authenticate the request using each authentication instance
            in turn.
            """
            # [BaseAuthentication对象,]
            # 循环认证类的所有对象
            for authenticator in self.authenticators:
                try:
                    # 执行认证类的authenticate方法
                    # 1.如果auth方法抛出异常,self._not_auth()执行
                    # 2.有返回值,必须得是元祖(request.user, request.auth)
                    # 3.返回None , 当前不处理,下一个认证来处理
                    user_auth_tuple = authenticator.authenticate(self)
    

    返回的元祖,(user和auth)

    传的是类,怎么调用的方法?

    authenticators=self.get_authenticators(), #[BasicAuthentication(),],把对象封装到request里面了

    ​ def get_authenticators(self):

    # self.authentication_classes = [foo,bar]

    ​ return [auth() for auth in self.authentication_classes] #变成对象的

    authentication_classes = api_settings.DEFAULT_AUTHENTICATION_CLASSES# 全局的但是里面的方法不知道怎么实现的

    局部: authentication_classes = [FirstAuthtication, Authtication,] #第一个什么也不干 (局部的可以,有则会调用到了)

    REST_FRAMEWORK = {
    全局使用的认证类
    'DEFAULT_AUTHENTICATION_CLASSES': ['app01.utils.auth.FirstAuthtication', 'app01.utils.auth.Authtication'],

    class Request:
    		.....
            self.authenticators = authenticators or ()
            ......
    	   # [BaseAuthentication对象,]
            # 循环认证类的所有对象
            for authenticator in self.authenticators:
                try:
                    # 执行认证类的authenticate方法
                    # 1.如果auth方法抛出异常,self._not_auth()执行
                    # 2.有返回值,必须得是元祖(request.user, request.auth)
                    # 3.返回None , 当前不处理,下一个认证来处理
                    user_auth_tuple = authenticator.authenticate(self)
    
    全局的怎么调用到的?

    "detail": "Method "GET" not allowed."

    没有定义这个方法

    认证流程:

    self.dispatch
      # 1.封装Request
            request = self.initialize_request(request, *args, **kwargs)
            
            
    		def initialize_request(self, request, *args, **kwargs):
    			parser_context = self.get_parser_context(request)
    			return Request(
                    request,
                    parsers=self.get_parsers(),
                    authenticators=self.get_authenticators(),	#返回的是对象
                    negotiator=self.get_content_negotiator(),
                    parser_context=parser_context
          		  )
     
            (1)然后在class Request:
     		self.authenticators = authenticators or ()
            
            def get_authenticators(self):
            # self.authentication_classes = [foo,bar]
            return [auth() for auth in self.authentication_classes]  # 成对象
            
            class APIView(View):
            authentication_classes = api_settings.DEFAULT_AUTHENTICATION_CLASSES
            
            局部配置
            authentication_classes = [FirstAuthtication, Authtication,] # 第一个什么也不敢
    		settings全局配置(我不懂,这里是怎么调用的,而且是比局部配置低优先级的)
    		REST_FRAMEWORK = {
        # 全局使用的认证类
        'DEFAULT_AUTHENTICATION_CLASSES': ['app01.utils.auth.FirstAuthtication', 'app01.utils.auth.Authtication'],
    		
    
        # 4.实现认证
            self.perform_authentication(request)
            
            
        def perform_authentication(self, request):
            request.user        
            
        def user(self):
            if not hasattr(self, '_user'):
                with wrap_attributeerrors():
                    self._authenticate()
            return self._user        
            
    	
    	(2)然后在class Request:    self.authenticators:(对象列表)      
        def _authenticate(self):
            # [BaseAuthentication对象,]
            # 循环认证类的所有对象
            for authenticator in self.authenticators:
                try:
                    # 执行认证类的authenticate方法
                    # 1.如果auth方法抛出异常,self._not_auth()执行
                    # 2.有返回值,必须得是元祖(request.user, request.auth)
                    # 3.返回None , 当前不处理,下一个认证来处理
                    user_auth_tuple = authenticator.authenticate(self)    
                    
                    
    
        def authenticate(self, request):
            return (self.force_user, self.force_token)
            
    

    1569060452911

    auth对象的传递

    1569061071441

    ![img](file:///F:UserDataMy DocumentsTencent Files1916910438ImageGroupImage9M(RO3LY[)2H)0K4J@JYZ5WW.png)

    https://www.bilibili.com/video/av28871471?from=search&seid=9790221487714052991&tdsourcetag=s_pcqq_aiomsg
    _(424662508) 21:54:52@all 分享大家一部分学习视频: django rest framework: https://www.bilibili.com/video/av28871471?from=search&seid=9790221487714052991 Git实战 见飞秋 路飞学城 https://www.bilibili.com/video/av58974283?from=search&seid=7694482453893961799 ES视频 链接: https://pan.baidu.com/s/1o8lV1fikJiOFh5dil39yug 提取码: bqc2
    _(424662508) 21:55:01@佐恩
    img2019-09-09img
    _(424662508) 16:11:37https://www.cnblogs.com/wupeiqi/articles/8184686.html
    _(424662508) 16:13:25https://www.cnblogs.com/wupeiqi/articles/6216618.html

    Flask全套组件及原理剖析https://www.bilibili.com/video/av28875133

    django rest framework 实战和源码剖析 https://www.bilibili.com/video/av28871471?from=search&seid=9790221487714052991

  • 相关阅读:
    Ajax beforeSend和complete 方法与防止重复提交
    tablesorter周边文档
    对委托的一些短浅理解
    Nginx核心要领五:worker_processes、worker_connections设置
    二进制安装k8s 教程
    安装 Docker Engine-Community
    centos7.x 安装 NodeJS、yarn、pm2
    cfssl
    k8s各个进程 占用内存大小
    Linux下查看某一进程所占用内存的方法
  • 原文地址:https://www.cnblogs.com/Doner/p/11564085.html
Copyright © 2011-2022 走看看