from django.utils.deprecation import MiddlewareMixin from django.shortcuts import redirect import re class PermissionMiddleware(MiddlewareMixin): def process_request(self, request): curr_path = request.path print(curr_path) # 白名单 white_list = ['/myuser_login/', '/myuser_reg'] for w in white_list: if re.search(w, curr_path): return None # 验证登录 print(request.user.is_authenticated) if not request.user.is_authenticated: return redirect('/myuser_login/')