》请求流程
》通过中间件,过滤黑名单用户
from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import HttpResponse
class MyTestMiddleware(MiddlewareMixin):
black_list = ["127.0.0.1"]
def __init__(self, getresponse):
print('at init', getresponse)
self.get_response = getresponse
print(111)
# return self.get_response
def process_view(self,request, view_func, *view_args, **view_kwargs):
print('URL后,VIEW前,执行process_view')
def process_request(self, request):
print("收到请求,产生request对象后,process_request")
print(request.META.get("REMOTE_ADDR"))
remote_ip = request.META.get("REMOTE_ADDR")
if remote_ip in MyTestMiddleware.black_list:
return HttpResponse("非法用户,禁止通行")
def process_response(self,request,response):
print("产生响应对象,返给客户前,process_response ")
return response