Python Day 65 Django framework, Django life cycle, Django middleware, middleware implementation process, Django model and MTV in the MVC pattern

  ## Django life cycle

 

 

  ## Django middleware

# Django middleware in 
open Django project Settings.py file, which is middleware configuration items MIDDLEWARE 
MIDDLEWARE = [
     ' django.middleware.security.SecurityMiddleware ' ,
     ' django.contrib.sessions.middleware.SessionMiddleware ' ,
     ' Django. middleware.common.CommonMiddleware ' ,
     ' django.middleware.csrf.CsrfViewMiddleware ' ,
     ' django.contrib.auth.middleware.AuthenticationMiddleware ' ,
     ' django.contrib.messages.middleware.MessageMiddleware ' ,
     'django.middleware.clickjacking.XFrameOptionsMiddleware ' , 
] 


MIDDLEWARE configuration items is a list (the list is ordered, keep that in mind, you'll know why later ordered to emphasize the word), the list is a string, these In fact, a string is a class, which is a mezzanine.

  ## custom middleware

# 1, middleware defines five methods, respectively, are the main :( process_request and process_response) 
1 , process_request (Self, Request)
 2 , process_view (Self, Request, view_func, view_args, view_kwargs)
 . 3 , process_template_response (Self, Request, Response)
 . 4 , process_exception (Self, Request, Exception)
 . 5 , process_response (Self, Request, Response) 

returns the value of the above method may be a HttpResponse object None or, if is None, then continues according to the rules defined in the rearward django continue, if HttpResponse object, the object is returned directly to the user. 


# 2, process_request method 
process_request has one parameter, the request, the request and view function is the same as in the request (prior to routing Django later, this request object on a series of operations). 

Since the request object is the same, so we can target to request a series of operations, including the request. Variable name =Variable value, such operations, we can obtain the same manner as we set to a value in the middleware functions in the subsequent view. 

Its return value can be None can be HttpResponse object. The return value is None, then the normal process to continue to go to the next middleware processing, if it is HttpResponse object, Django view function will not be performed, and the corresponding object is returned to the browser. 

# 3, process_response method 
process_response method multiple middleware is registered in accordance with MIDDLEWARE in reverse order of execution, that is the first middleware process_request method is performed first, and its last execution method process_response last Middleware process_request a method of last execution, its process_response method is executed first. 

# . 4, process_view method 
process_view (self, request, view_func, view_args, view_kwargs) 

which has four parameters

 - Request is HttpRequest object.
- view_func view function, Django is about to use. (Which is actually a function of the object, rather than as a function of the name string.)
- view_args position parameter list is passed to the view.
 - view_kwargs keyword dictionary is to pass parameters to the view. view_args and view_kwargs not contain the first view parameter (request).

Django calls the view process_view method before calling the function. 

It should return None or an HttpResponse object. If it returns None, Django will continue to process the request, the middleware process_view perform any other method, and then perform a corresponding view. If it returns an HttpResponse object, Django will not perform the function of a view, but the U-turn directly in middleware, flashbacks perform one process_response method, and finally returned to the browser

  ## middleware implementation process

After the request arrives at the middleware, the implementation of the first intermediate process_request method according to each registered positive sequence, process_request method returns the value of None, it is sequentially performed, if the value returned is HttpResponse object, the latter method is not executed process_request, but process_response execution of the current method corresponds middleware (Note that not all process_response method to perform a U-turn), the HttpResponse object returned to the browser. In other words: If MIDDLEWARE registered six middleware, the implementation process, the first three middleware returns an HttpResponse object, 4,5,6 middleware process_request and process_response methods are not implemented, the implementation of the order of 3 , process_response method 2,1 middleware.

 

 

process_request methods are executed, the matching route, find the view functions to be performed, the first view is not executed function, perform process_view method middleware, process_view method returns None, continue with the order, all process_view method of execution after execution view function. If intermediate 3 process_view method returns HttpResponse object, and the view of the 4,5,6 process_view functions are not executed, from the last intermediate, intermediate process_response method is reverse 6 begins execution directly.

   ## Django flowchart request

  

 

  ##Django中MTV模式 和 MVC模式

MVC:
    
    项目目录结构的设计模式
    
    
    客户       ------点餐-------->    服务员  ------处理菜单需求--------> 厨师
               <------------------            <-------------------------        
    
    (浏览器)  ------------------->  函数或者类处理  ------------------>  数据库
                                       业务逻辑
    views:                         controllers                          models:
                                        loginController.py                    LoginModel.py
                                        UserController.py                   UserModel.py
        大量html页面                         
        
    
MVC
    
    django:
        M : models 
        T : Templates (各种html页面) 相当于views
        V :Views(视图处理函数) 相当于 controllers
    

 

Guess you like

Origin www.cnblogs.com/liangzhenghong/p/11210355.html