django中间件

参考博客:https://www.cnblogs.com/liwenzhou/p/8761803.html

1. 今日内容

         中间件:http://www.cnblogs.com/liwenzhou/p/8761803.html

        

                   1. URL的白名单 url = ["/xx/", "/oo/", "/haha/"]

                   2. 登陆之后才能访问某些URL    之前使用装饰器

 

        

         中间件的定义:

                   wsgi之后 urls.py之前 在全局 操作Django请求和响应的模块!

                  

         中间件的使用:

                   5个固定的方法

                            process_request(self, request)

                                     执行顺序:

                                               按照注册的顺序(在settings.py里面设置中 从上到下的顺序)

                                     何时执行:

                                               请求从wsgi拿到之后

                                     返回值:

                                               返回None,继续执行后续的中间件的process_request方法

                                               返回response , 不执行后续的中间件的process_request方法

                                              

                            process_response

                                     执行顺序:

                                               按照注册顺序的倒序(在settings.py里面设置中 从下到上的顺序)

                                     何时执行:

                                               请求有响应的时候

                                     返回值:

                                               必须返回一个response对象

                                              

                            process_view(self, request, view_func, view_args, view_kwargs):

                                     执行顺序:

                                               按照注册的顺序(在settings.py里面设置中 从上到下的顺序)

                                     何时执行:

                                               在urls.py中找到对应关系之后 在执行真正的视图函数之前

                                     返回值:

                                               返回None,继续执行后续的中间件的process_view方法

                                               返回response,

                                              

                            process_exception(self, request, exception)

                                     执行顺序:

                                               按照注册顺序的倒序(在settings.py里面设置中 从下到上的顺序)

                                     何时执行:

                                               视图函数中抛出异常的时候才执行

                                     返回值:

                                               返回None,继续执行后续中间件的process_exception

                                               返回response,

                                              

                                              

                            process_template_response(self, request, response)

                                     执行顺序:

                                               按照注册顺序的倒序(在settings.py里面设置中 从下到上的顺序)

                                     何时执行:

                                     视图函数执行完,在执行视图函数返回的响应对象的render方法之前

                                     返回值:

                                               返回None,继续执行后续中间件的process_exception

                                               返回response,

        

        

                   Django调用 注册的中间件里面五个方法的顺序:

                            1. process_request

                                     urls.py

                            2. process_view

                                     view

                            3. 有异常就执行 process_exception

                            4. 如果视图函数返回的响应对象有render方法,就执行process_template_response

posted @ 2019-07-23 10:32  【小桥流水人家】  阅读(101)  评论(0编辑  收藏  举报