寫了幾週的腳本,今天終於開始接觸web框架了~學習Python的web框架,那麼Django就幾乎是必修課了,這次的工作是先打打下手,主要的任務是在setting中新增版本號,在渲染靜態css,js的路徑時附帶版本號,例如“example.v1124.css”,然後再在request請求中祛除掉版本號。為什麼要這麼做呢?因為這樣做的話在前端輸出靜態檔案的路徑就會加上就會加上版本號,這樣當開發出新的靜態檔案的時候,客戶端就會強制刷新本地的緩存,為了達到這個目的就要首先要在settings檔案中配置,這樣就可以每次修改的時候直接從settings讀取版本號了
我們從瀏覽器發出一個請求Request,得到一個回應後的內容HttpResponse ,這個請求傳遞到Django的過程如下:
也就是說,每一個請求都是先透過中間件中的process_request 函數,這個函數返回None 或HttpResponse 對象,如果返回前者,繼續處理其它中間件,如果返回一個HttpResponse,就處理中止,返回到網頁上。
#在project根目錄下建立中間件py檔:
1 class RequestExeute(object): 2 def process_request(self, request): 3 print('process_request') 4 5 def process_view(self, request, callback, callback_args, callback_kwargs): 6 print('process_view') 7 8 def process_exception(self, request, exception): 9 """10 当views函数出错时执行11 :param request: 12 :param exception: 13 :return: 14 """15 print('process_exception')16 17 def process_response(self, request, response):18 """19 必须return HttpResponse20 :param request: 21 :param response: 22 :return: 23 """24 print('process_response')25 return response26 27 def process_template_response(self, request, response):28 """29 视图函数的返回值中,如果有render方法,才被调用30 :param request:31 :param response:32 :return:33 """34 print('process_template_response')35 return response
在settings.py檔案中,依照欲執行的順序註冊:
ps. 在1.9及之前的版本中,中間件的關鍵字為:MIDDLEWARE_CLASSES
MIDDLEWARE_CLASSES = ('zqxt.middleware.BlockedIpMiddleware', ...其它的中间件 )
1 from django.shortcuts import HttpResponse 2 from django.utils.deprecation import MiddlewareMixin 3 class M1(MiddlewareMixin): 4 def process_request(self, request): 5 print('m1.request') 6 # return HttpResponse('request_m1') 7 8 def process_view(self, request, callback, callback_args, callback_kwargs): 9 print('m1.view')10 # response = callback(request, *callback_args, **callback_kwargs)11 # return response12 13 def process_response(self, request, response):14 print('m1.response')15 return response16 17 def process_exception(self, request, exception):18 print('m1.process_exception')19 return HttpResponse('m1.process_exception')20 21 def process_template_response(self, request, response):22 """23 视图函数的返回值中,如果有render方法,才被调用24 :param request:25 :param response:26 :return:27 """28 print('m1.process_template_response')29 return response30 31 32 class M2(MiddlewareMixin):33 def process_request(self, request):34 print('m2.request')35 36 def process_view(self, request, callback, callback_args, callback_kwargs):37 print('m2.view')38 # response = callback(request, *callback_args, **callback_kwargs)39 # return response40 41 def process_response(self, request, response):42 print('m2.response')43 return response44 45 def process_exception(self, request, exception):46 print('m2.process_exception')47 return HttpResponse('m2.process_exception')48 49 def process_template_response(self, request, response):50 """51 视图函数的返回值中,如果有render方法,才被调用52 :param request:53 :param response:54 :return:55 """56 print('m2.process_template_response')57 return response
執行順序:
m1.request
m2.request
若process_request返回HttpResponse 對象,則從目前中間件的process_response向前返回
1 from django.shortcuts import HttpResponse 2 from django.utils.deprecation import MiddlewareMixin 3 class M1(MiddlewareMixin): 4 def process_request(self, request): 5 print('m1.request') 6 return HttpResponse('request_m1') 7 8 def process_view(self, request, callback, callback_args, callback_kwargs): 9 print('m1.view')10 # response = callback(request, *callback_args, **callback_kwargs)11 # return response12 13 def process_response(self, request, response):14 print('m1.response')15 return response16 17 def process_exception(self, request, exception):18 print('m1.process_exception')19 return HttpResponse('m1.process_exception')20 21 def process_template_response(self, request, response):22 """23 视图函数的返回值中,如果有render方法,才被调用24 :param request:25 :param response:26 :return:27 """28 print('m1.process_template_response')29 return response30 31 32 class M2(MiddlewareMixin):33 def process_request(self, request):34 print('m2.request')35 36 def process_view(self, request, callback, callback_args, callback_kwargs):37 print('m2.view')38 # response = callback(request, *callback_args, **callback_kwargs)39 # return response40 41 def process_response(self, request, response):42 print('m2.response')43 return response44 45 def process_exception(self, request, exception):46 print('m2.process_exception')47 return HttpResponse('m2.process_exception')48 49 def process_template_response(self, request, response):50 """51 视图函数的返回值中,如果有render方法,才被调用52 :param request:53 :param response:54 :return:55 """56 print('m2.process_template_response')57 return response58 59 60 # class RequestExeute(object):61 # def process_request(self, request):62 # print('process_request')63 #64 # def process_view(self, request, callback, callback_args, callback_kwargs):65 # print('process_view')66 #67 # def process_exception(self, request, exception):68 # """69 # 当views函数出错时执行70 # :param request:71 # :param exception:72 # :return:73 # """74 # print('process_exception')75 #76 # def process_response(self, request, response):77 # """78 # 必须return HttpResponse79 # :param request:80 # :param response:81 # :return:82 # """83 # print('process_response')84 # return response85 #86 # def process_template_response(self, request, response):87 # """88 # 视图函数的返回值中,如果有render方法,才被调用89 # :param request:90 # :param response:91 # :return:92 # """93 # print('process_template_response')94 # return response
1 from django.shortcuts import HttpResponse 2
from django.utils.deprecation import MiddlewareMixin 3 class M1(MiddlewareMixin): 4
def process_request(self, request): 5
print('m1.request') 6
# return HttpResponse('request_m1') 7 8
def process_view(self, request, callback, callback_args, callback_kwargs): 9
print('m1.view')10 response = callback(request, *callback_args, **callback_kwargs)11
return response12 13 def process_response(self, request, response):14 print('m1.response')15
return response16 17 def process_exception(self, request, exception):18
print('m1.process_exception')19
return HttpResponse('m1.process_exception')20 21 def process_template_response(self, request, response):22 """23 视图函数的返回值中,如果有render方法,才被调用24 :param request:25 :param response:26 :return:27 """28 print('m1.process_template_response')29 return response30 31 32 class M2(MiddlewareMixin):33 def process_request(self, request):34 print('m2.request')35 36 def process_view(self, request, callback, callback_args, callback_kwargs):37 print('m2.view')38 # response = callback(request, *callback_args, **callback_kwargs)39 # return response40 41 def process_response(self, request, response):42 print('m2.response')43 return response44 45 def process_exception(self, request, exception):46 print('m2.process_exception')47 return HttpResponse('m2.process_exception')48 49 def process_template_response(self, request, response):50 """51 视图函数的返回值中,如果有render方法,才被调用52 :param request:53 :param response:54 :return:55 """56 print('m2.process_template_response')57 return response
m1.request m2.request m1.view m2.response m1.respons
以上是Django中間件的詳細介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!