APIView of dispatch

(1) dispatch method Detailed request object original package ----

(Original request methods and properties can be encapsulated in the call request directly or can be used request._request, such as: request.user == request._request.user

DEF dispatch (Self, Request, * args, ** kwargs):
     "" " 
    ` .dispatch () `Pretty much at The Same, AS IS Django's Regular dispatch, 
    . But with Extra Hooks for the Startup, the Finalize, and Exception Handling 
    " "" 
    self.args = args 
    self.kwargs = kwargs
     # 1. further request to the original package 
    request = self.initialize_request (request, args *, ** kwargs) 
    self.request = request # (2) has been subjected to further request package the 
    self.headers = self.default_response_headers   # ? deprecate 
 
    the try :
 # 2. adding a call to request the
        self.initial(request, *args, **kwargs)#(3)比View中多的执行的方法,使用封装过后的request进行调用
 
        # Get the appropriate handler method
        if request.method.lower() in self.http_method_names:
            handler = getattr(self, request.method.lower(),
                              self.http_method_not_allowed)
        else:
            handler = self.http_method_not_allowed
 
        response = handler(request, *args, **kwargs)
 
    except Exception as exc:
        response = self.handle_exception(exc)
 
    self.response Self.finalize_response = (Request, Response, args *, ** kwargs) # (. 4) in response to the original object is further encapsulated 
    return self.response
dispatch method overrides

(2) initialize_request original request for encapsulation  

DEF initialize_request (Self, Request, * args, ** kwargs):
     "" " 
    . Returns at The Initial Request Object 
    " "" 
    parser_context = self.get_parser_context (Request) 
 
    return Request ( 
        Request, # (1-1) the original Request 
        parsers self.get_parsers = (), # (1-2) [deserialization mode list] 
        authenticators = self.get_authenticators (), # (1-3) example of the configuration file called rest_framework [middleware authentication class list] - - [BasicAuthentication objects, SessionAuthentication Object] 
        Negotiator = self.get_content_negotiator (), 
        parser_context = parser_context 
    )
initialize_request封装request

(3) self.initial Detailed ---- certification authority + + + version control throttle  

self.initial(request, *args, **kwargs)#(3)比View中多的执行的方法,使用封装过后的request进行调用

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.(3-0)api版本的获取
    Version, = self.determine_version scheme (Request, * args, ** kwargs) 
    request.version, request.versioning_scheme = Version, scheme # version number and processed 

    # Ensure® that incoming Request The IS permitted 
    self.perform_authentication (Request) # (3-1) authentication is logged 
    self.check_permissions (Request) # (3-2) rights component 
    self.check_throttles (Request) # frequency components
Detailed self.initial

 

Guess you like

Origin www.cnblogs.com/open-yang/p/11572886.html