#視圖 class login (View): pass #路由 url(r'^books/$', views.login.as_view())
#閱讀源碼: #左側工程欄--->設置圖標-->點擊--->show members(能看到py文件,pu下的類,類下的方法) -Class Base View(基於類的視圖) -Function Base View(基於函數的視圖) -def as_view 類方法 :返回view -def view:as_view的內的函數(閉包)
-python中一切皆對象:函數也是對象 -hasattr(self, 'get')--判斷self類中是否是有該(get)方法 -setattr(self,get,get_all):至關於把get函數,變成了get_all -getattr(self, 'get'):拿到get函數的內存地址 - def view(request, *args, **kwargs): self = cls(**initkwargs) if hasattr(self, 'get') and not hasattr(self, 'head'): self.head = self.get self.request = request self.args = args self.kwargs = kwargs #執行:dispatch:誰的dispatch方法?寫的cbv的那個c,視圖中的那個視圖類 #我這個類若是沒有寫dispatch,會執行View中的dispatch方法 return self.dispatch(request, *args, **kwargs) -def dispatch(self, request, *args, **kwargs): #request.method 前臺請求的方法,轉成了小寫 #http_method_names View中定義的一個列表:是一堆請求方式 if request.method.lower() in self.http_method_names: #getattr的第三個參數是默認值:self.http_method_not_allowed #拿到get方法的內存地址 handler = getattr(self, request.method.lower(), self.http_method_not_allowed) else: handler = self.http_method_not_allowed #get(request,*args, **kwargs) return handler(request, *args, **kwargs
總結:*******請求來了--->as_view---->view---->dispatch--->分發到不一樣的函數,執#行函數,拿到結果python
安裝:djangorestframework
-它是一個app,要在咱的項目中用
-只是快速的構建resful規範的接口
-csrf_exempt:局部禁用csrf(csrf是能夠局部使用,局部禁用)
-之後再執行的dispatch方法是APIView的dispatch方法
-getattr和setattr
重點掌握這三點:
-request.data 是個方法,包裝成了屬性,前臺傳過來body體中數據的數據,放在裏面
-request.query_params 這個是原來GET中的數據
-request把原來的request包裝進去了django
@classmethod def as_view(cls, **initkwargs): """ Store the original class on the view function. This allows us to discover information about the view when we do URL reverse lookups. Used for breadcrumb generation. """ if isinstance(getattr(cls, 'queryset', None), models.query.QuerySet): def force_evaluation(): raise RuntimeError( 'Do not evaluate the `.queryset` attribute directly, ' 'as the result will be cached and reused between requests. ' 'Use `.all()` or call `.get_queryset()` instead.' ) cls.queryset._fetch_all = force_evaluation 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)
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
def initialize_request(self, request, *args, **kwargs): """ Returns the initial request object. """ 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 )
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)
總結:*******請求來了--->as_view---->view---->dispatch(apiview的比上面的多作了 包了個request對象生成新的request對象 和調用initial方法 都是apiview本身的方法)--->分發到不一樣的函數,執#行函數,拿到結果api