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")

 

Django 拦截器

上一篇:最大似然估计和朴素贝叶斯


下一篇:Tomcat部署及优化