首页 > 其他 > 详细

Django 拦截器

时间:2021-08-17 10:28:10      阅读:27      评论:0      收藏:0      [点我收藏+]

创建拦截器后,所有通信都会先走拦截器,然后再去访问urls

django中拦截器的创建

#### setting 中配置
MIDDLEWARE_CLASSES = [
‘django.middleware.security.SecurityMiddleware‘,
‘django.contrib.sessions.middleware.SessionMiddleware‘,
‘django.middleware.common.CommonMiddleware‘,
‘django.middleware.csrf.CsrfViewMiddleware‘,
‘django.contrib.auth.middleware.AuthenticationMiddleware‘,
‘django.contrib.auth.middleware.SessionAuthenticationMiddleware‘,
‘django.contrib.messages.middleware.MessageMiddleware‘,
‘django.middleware.clickjacking.XFrameOptionsMiddleware‘,
‘documents.myMiddleware.FilterMiddleware‘, # 配置拦截器路径
]

#### myMiddleware 拦截器文件
# -*- coding: utf-8 -*-

from django.http import HttpResponseRedirect,HttpResponse

from django.shortcuts import render
try:
from django.utils.deprecation import MiddlewareMixin # Django 1.10.x
except ImportError:
MiddlewareMixin = object # Django 1.4.x - Django 1.9.x



class FilterMiddleware(MiddlewareMixin):
def process_request(self, request):
print request.path
# 填上所有不拦截的一级路由
if request.path.startswith("/xxx") == True or request.path.startswith("/xxx") == True or request.path.startswith("/xxx") == True or request.path.startswith("/xxx") == True or request.path.startswith("/xxx") == True or request.path.startswith("/xxx") == True or request.path.startswith("/xxx") == True:
pass
else:
# return HttpResponse("404")
return render(request, "xxx/xxx/404.html")

 

Django 拦截器

原文:https://www.cnblogs.com/yanruizhe/p/15150853.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!