中间件
from django.middleware.csrf import CsrfViewMiddleware #中间间的本质就是类
MIDDLEWARE = [
‘md.middleware.M1‘, #导入md文件夹middleware.py下的M1类
‘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‘,
‘md.middleware.M2‘,
]
from django.utils.deprecation import MiddlewareMixin #导入CsrfViewMiddleware内部继承的类
class M1(MiddlewareMixin):
def process_request(self,request): #定义自己的process_request方法
print("m1.process_request")
def process_response(self,request,response): #定义自己的process_response方法
print("m1.process_response")
return response
# 源码为def process_view(self,request,callback,callback_args,callback_kwargs):
# 视图函数 视图函数的args 视图函数的kwargs
def process_view(self,request,view_func,view_func_args,view_func_kwargs):
print("m2.process_view")
class M2(MiddlewareMixin):
def process_request(self,request):
print("m2.process_request")
def process_response(self,request,response):
print("m2.process_response")
return response
def process_view(self,request,view_func,view_func_args,view_func_kwargs):
print("m2.process_view")
#执行结果依次为
m1.process_request
m2.process_request
m1.process_view
m2.process_view
m2.process_response
m1.process_response
process_view的执行顺序(执行完路由匹配后自上而下执行) |
|
process_view的返回顺序 |
|
process_exception
|
|
process_template
返回的对象有render方法才被执行 |
|
|
ps:
博客园中有两种插入代码的方式
针对推荐的插入代码方式, 可以使用如下代码快速展开折叠的代码
$(‘div.cnblogs_code_hide‘).click();
针对另一种插入代码的方式, 可以使用如下代码快速展开折叠的代码
$(‘div.syntaxhighlighter‘).removeClass(‘collapsed‘);
process_view