django中间件
中间件可以控制浏览器发送request请求到达view视图之前和response到达浏览器之前动作
from django.http import HttpResponse
EXCLUDE_IPS = ['127.0.0.2']
def process_view(get_response):
def middleware(request):
ip = request.META['REMOTE_ADDR']
if ip in EXCLUDE_IPS:
return HttpResponse('拒绝访问')
'''request到view之前的动作'''
response = get_response(request) #以response为分界线
'''response到达浏览器之前的动作'''
return response
return middleware