Django 攔截器


創建攔截器后,所有通信都會先走攔截器,然后再去訪問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")

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM