from django.utils.deprecation import MiddlewareMixin from django.shortcuts import render,redirect class UserMiddleware(MiddlewareMixin):#django中有三个中间键 def process_request(self,request): path = request.path_info if path == '/SS01/login/' or path == '/SS01/register/': return None else: if not request.session.get('username'):# 如果进入了/SS01/index这样的地址 return redirect('/SS01/login/') else: return None def process_view(self,request,callback,callback_args,callback_kwargs): pass def process_response(self,request,response): return response