Detailed process framework Django Rest

  1. What is Restful
    • REST has nothing to do with technology, represent a style of software architecture, REST is Representational State Transfer short, the Chinese translation of "transfer of state characterized" as
    • REST look like from the perspective of resources throughout the network, which will be identified in the distribution of resources in the network of a node by its URL, the client application to obtain the resources characterized by URL, access to these applications characterize the resulting change the status of these
    • All of the data, but was acquired by the network or the operation (CRUD) data, all resources, all data will be treated as REST resources are the most essential attribute different from other architectural styles
    • For this resource-oriented REST architectural style, it was suggested that a new structure concept, namely: resource-oriented architecture (ROA: Resource Oriented Architecture)
    • Visit a website, an interactive process on behalf of the client and the server, the client means used, only the HTTP protocol. Specifically, it is inside the HTTP protocol, four verb indicates the operation mode: GET, POST, PUT, DELETE. Which correspond to the four basic operations: GET used to obtain resources, POST for new resources (can also be used to update the resource), PUT for updating resources, DELETE to delete the resource.
    • Integrated the above explanation, we summarize what is RESTful architecture:
    • Each URI represents a resource;
    • Between the client and the server, passing some kind of presentation layer of such resources;
    • Four client via HTTP verbs, operation of server resources, to achieve "the state of the presentation layer conversion.

 

 

  2. The   process client requests access to data

    • client request issued CRUD
    • urls ->views
    • views -> as_view
    • as_view ->dispatch

 

 

  3.as_view is the entry function

    • as_view here is the APIView
    • First as_view is a method of APIView, APIView inherited from the View, is also a method that is overridden as_view
    • as_view do queryset detection, and prompt us not queryset direct operation, direct manipulation of data between multiple requests occur disorder
    • Recommended for all or get_queryset
    • Next call parent class as_view, and made csrf exemption, as reflected in the source code:
      •         view = super(APIView, cls).as_view(**initkwargs)
                view.cls = cls
                view.initkwargs = initkwargs
        
                # Note: session based authentication is explicitly CSRF validated,
                # all other authentication is CSRF exempt.
                return csrf_exempt(view)
        

         

      • Call the parent class as_view, were
        • Parameter detection
        • Defines the closure function
        • Record data
        • Then dispatch (dispatch APIView here is the dispatch, the following source code)
        •     def dispatch(self, request, *args, **kwargs):
                  """
                  `.dispatch()` is pretty much the same as Django's regular dispatch,
                  but with extra hooks for startup, finalize, and exception handling.
                  """
                  self.args = args
                  self.kwargs = kwargs
                  request = self.initialize_request(request, *args, **kwargs)
                  self.request = request
                  self.headers = self.default_response_headers  # deprecate?
          
                  try:
                      self.initial(request, *args, **kwargs)
          
                      # 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)
                  return self.response

           

  4.dispatch

    • Overriding methods
    • I made a request conversion
    • After the whole initialization
      • Formatting suffix
      • Content Decision
      • Version of the decision-making (according to custom requirements)
        • Mandatory upgrade
        • Recommended upgrade
        • Or do not upgrade
      • Perform authentication
      • Check permissions
      • Throttling
      • This reflects the overall initialization command, the initial dispatch call method dispatch function, initial data request in the initializing operation
      •   try:
                    self.initial(request, *args, **kwargs)

         



      • 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) 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)

         

 

    • After finished the initialization of the data, according to a request for distribution method
    • If an exception occurs above process, the exceptions do not directly cast, it will be exception caught
    • Provide policy exception
    • The corresponding final uniform treatment
    • Finally, return

Guess you like

Origin www.cnblogs.com/wuygblog/p/10990432.html