• 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

  • 相关阅读:
    Method总结
    使用CSS的五种方式
    debug js code
    Overload
    fiddler模拟弱网测试
    POJ 1753 Flip Game (IDA*)
    UVA 11400 Lighting System Design(照明系统设计)(dp)
    UVA 12563 Jin Ge Jin Qu hao(劲歌金曲)(01背包+滚动数组)
    UVA 116 Unidirectional TSP (单向TSP)(dp多段图的最短路)
    UVA 1151 Buy or Build (买还是建)(并查集+二进制枚举子集)
  • 原文地址:https://www.cnblogs.com/geng-xiaoqiaoliushui/p/11230243.html
Copyright © 2020-2023  润新知