中间件:
process_request(self,request)
参数:
request 请求的对象,和视图中的request是同一个
执行时间:视图函数之前
执行顺序:
按照注册的顺序 顺序执行
返回值
None : 正常流程
HttpReponse: 当前中间件之后的中间件的process_request方法、路由匹配、视图也不执行,直接执行当前中间件的process_response方法
settings:
"""
Django settings for about_middleware project.
Generated by 'django-admin startproject' using Django 1.11.26.
For more information on this file, see
https://docs.djangoproject.com/en/1.11/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.11/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '^t344mb401h!=^hd&igb4_-39*z$ldlb(0$4a==4r7lu+6)yb8'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app01.apps.App01Config',
]
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.middlewares.my_middleware.MD1', #注册中间件1
'app01.middlewares.my_middleware.MD2', #注册中间件2
]
ROOT_URLCONF = 'about_middleware.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join ( BASE_DIR, 'templates' )]
,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'about_middleware.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/
STATIC_URL = '/static/'
定义中间件my_middleware:
from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import HttpResponse
class MD1(MiddlewareMixin):
def process_request(self,request):
# print(id(request))
# request.name = "alexdsb"
print(" MD1 process_request ")
return HttpResponse("md1 response")
class MD2(MiddlewareMixin):
def process_request(self,request):
print("MD2 process_request")
process_response(self, request, response)
参数:
request 请求的对象,和视图中的request是同一个
response 返回的响应对象
执行时间:视图函数之后
执行顺序:
按照注册的顺序 倒叙执行
返回值
HttpReponse: 必须返回响应对象
process_view(self, request, view_func, view_args, view_kwargs)
参数:
request 请求的对象,和视图中的request是同一个
view_func 视图函数
view_args 视图函数的位置参数
view_kwargs 视图函数的关键字参数
执行时间:路由匹配之后,视图函数之前
执行顺序:
按照注册的顺序 顺序执行
返回值
None 正常流程
HttpReponse: 当前中间件之后的process_view、视图不执行,执行最后一个中间的process_response
process_exception(self, request, exception)
参数:
request 请求的对象,和视图中的request是同一个
exception 错误对象
执行时间(触发条件):视图层面有错误才执行
执行顺序:
按照注册的顺序 倒叙执行
返回值
None 当前中间没有处理异常,交由下一个中间件处理异常,所有的中间件都没有处理,Django处理错误
HttpReponse: 当前中间处理好异常,之后执行最后一个中间件的process_response方法
pocess_template_response(self,request,response)
参数:
request 请求的对象,和视图中的request是同一个
response 返回的响应对象
执行时间:视图必须返回一个template_response对象
执行顺序:
返回值
HttpReponse: 必须返回响应对象
response.template_name = 'index1.html' # 改模板
response.context_data # 改变量