Python-django Rest Framework Dispatch method source code Analysis

Source: Internet
Author: User
Tags auth

1.Django CBV in the request after the arrival of the implementation of the dispatch method, dispatch method according to the request mode of triggering get/post/put and other methods

classApiview (View):defDispatch (self, request, *args, * *Kwargs):#1.1 encapsulating the request of WsgiRequest = Self.initialize_request (Request, *args, * *Kwargs) Self.request= Request#the self.request at this time is the request object of Rest_framework, which has something more than the WSGI request.        Try:            #1.2 Initialization: Version, authentication, permissions, frequency of AccessSelf.initial (Request, *args, * *Kwargs)
#1.3 Reflection execution Get methods ifRequest.method.lower ()inchSelf.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)exceptException as Exc:response=self.handle_exception (EXC)#1.4 re-package the returned responseSelf.response = self.finalize_response (Request, Response, *args, * *Kwargs)#1.5 return returnSelf.response

Step 1.1:

 from  rest_framework.request import  Span style= "COLOR: #000000" > Request  class   Apiview (View):  def  initialize_request (self, request, *args, **kwargs): #返回了一个 rest_framework Request object   return   request (request, 
#1.1.1 p Arsers =self.get_parsers (),
#1.1.2 Authenticators =self.get_authenticators (),
#1.1.3 negotiator =self.get_content_negotiator (), )

1th. 1.1 Steps:

Pass

1th. 1.2 Steps:

class Apiview (View):     def get_authenticators (self):
#self. authentication_classes = api_settings. Default_authentication_classes is to take data from the configuration file, it is not difficult to see from the variable name that there may be a list of many classes return for in Self.authentication_classes]
#self.authenticators = A list of multiple objects

Step 1.2:

class Apiview (View):     def initial (self, request, *args, * *Kwargs):
#版本相关
Versionself.determine_version (Request, *args, **kwargs) #1.2.1 Certification related self.perform_authentication (Request) #1.2.2 Permissions related Self.check_permissions (Request) #1.2.3 Access frequency related self.check_throttles ( Request

1th. 2.1 Steps:

class Apiview (View):     def perform_authentication (self, request): #1.2.1.1        request.user

Step 1.2.1.1:

class Request (object):        @property    def  User:       #此时的self is rest_framework  Request Object        ifnot'_user'): With            Wrap _attributeerrors ():
#1.2.1.1.1 self._authenticate () return self._user

Step 1.2.1.1.1 :

classRequest (object):def_authenticate (self): #此时的self is the request object for Rest_framework
forAuthenticatorinchself.authenticators: #self.authenticators = A list of multiple objects Try:
# Execute the authenticate method for each object user_auth_tuple=authenticator.authenticate (self) #从变量的名不难看出 returns a tupleexceptexceptions. Apiexception:self._not_authenticated ()Raise ifUser_auth_tuple is notNone:self._authenticator=Authenticator
#赋值, request. User and request. Auth and return to Self.user, Self.auth=User_auth_tuplereturnself._not_authenticated ()

Step 1.3 : reflection performs a Get method

Finally, we can customize a simple user authentication

classMyauth (object):defAuthenticate (self,request):return "1111","222"    classHost (Apiview): Authentication_classes=[Myauth]defGet (self,request):Print(request.user) #1111Print(Request.auth) #222returnHttpResponse ("666")

Python-django Rest Framework Dispatch method source code Analysis

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.