django python登录拦截器登录过滤器中间件

无敌的宇宙
无敌的宇宙
擅长邻域:Java,HTML,JavaScript,MySQL,支付,退款,图片上传

分类: python 标签: django python登录拦截器登录过滤器中间件

2024-08-14 22:54:37 429浏览

django python登录拦截器登录过滤器中间件

目录

image.png

image.png

image.png


拦截器

from django.utils.deprecation import  MiddlewareMixin
from django.shortcuts import redirect

class LoginFilter(MiddlewareMixin):
def process_request(self,request):
url=request.path_info
print(url,"*"*10)
loginU=request.session.get('loginU')
if 'login' in url:
return
if loginU:
return
return redirect('/login')

def process_response(self,request,response):
print("loout")
return response
#setting.py注册中间
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'app01.middleware.filter.LoginFilter' #注册中间
]

好博客就要一起分享哦!分享海报

此处可发布评论

评论(0展开评论

暂无评论,快来写一下吧

展开评论

您可能感兴趣的博客

客服QQ 1913284695