Django中间件
基于装饰器
# views.py
1 from django.shortcuts import render, redirect, HttpResponse 2 from app01.forms import LoginForm 3 from app01 import models 4 from django.conf import settings 5 from utils.md5 import md5 6 7 8 def auth(func): 9 def inner(request,*args,**kwargs): 10 user_info = request.session.get(settings.USER_SEESION_KEY) 11 if not user_info: 12 return redirect('/login/') 13 res = func(*args,**kwargs) 14 return res 15 return inner 16 17 18 19 def login(request): 20 if request.method == 'GET': 21 form=LoginForm() 22 return render(request,'login.html',{'form':form}) 23 else: 24 form = LoginForm(request.POST) 25 if form.is_valid(): 26 form.cleaned_data['password'] = md5(form.cleaned_data['password']) 27 user = models.UserInfo.objects.filter(**form.cleaned_data).first() 28 if user: 29 request.session[settings.USER_SEESION_KEY] = {'id':user.pk,'username':user.username} 30 return redirect('/index/') 31 else: 32 form.add_error('password','用户名或密码错误') 33 return render(request,'login.html',{'form':form}) 34 35 36 @auth 37 def index(request): 38 return HttpResponse('welcome') 39 40 41 @auth 42 def host(request): 43 return HttpResponse('主机管理') 44 45 46 def testt(request): 47 print('views.test') 48 return HttpResponse('...')
基于中间件
# md.py
1 from django.conf import settings 2 from django.shortcuts import redirect 3 4 5 class MiddlewareMixin(object): 6 def __init__(self, get_response=None): 7 self.get_response = get_response 8 super(MiddlewareMixin, self).__init__() 9 10 def __call__(self, request): 11 response = None 12 if hasattr(self, 'process_request'): 13 response = self.process_request(request) 14 if not response: 15 response = self.get_response(request) 16 if hasattr(self, 'process_response'): 17 response = self.process_response(request, response) 18 return response 19 20 21 class M1(MiddlewareMixin): 22 23 def process_request(self, request, *args, **kwargs): 24 25 if request.path_info == '/login/': 26 return None 27 28 user_info = request.session.get(settings.USER_SEESION_KEY) 29 if not user_info: 30 return redirect('/login/') 31 32 def process_response(self, request, response): 33 print('m1.process_response') 34 return response
# views.py
from django.shortcuts import render, redirect, HttpResponse from app01.forms import LoginForm from app01 import models from django.conf import settings from utils.md5 import md5 def login(request): if request.method == 'GET': form=LoginForm() return render(request,'login.html',{'form':form}) else: form = LoginForm(request.POST) if form.is_valid(): form.cleaned_data['password'] = md5(form.cleaned_data['password']) user = models.UserInfo.objects.filter(**form.cleaned_data).first() if user: request.session[settings.USER_SEESION_KEY] = {'id':user.pk,'username':user.username} return redirect('/index/') else: form.add_error('password','用户名或密码错误') return render(request,'login.html',{'form':form}) def index(request): return HttpResponse('welcome') def host(request): return HttpResponse('主机管理') def testt(request): print('views.test') return HttpResponse('...')
中间件中其余的方法
from django.conf import settings from django.shortcuts import redirect, HttpResponse class MiddlewareMixin(object): def __init__(self, get_response=None): self.get_response = get_response super(MiddlewareMixin, self).__init__() def __call__(self, request): response = None if hasattr(self, 'process_request'): response = self.process_request(request) if not response: response = self.get_response(request) if hasattr(self, 'process_response'): response = self.process_response(request, response) return response class M1(MiddlewareMixin): def process_request(self, request, *args, **kwargs): if request.path_info == '/login/': return None user_info = request.session.get(settings.USER_SEESION_KEY) if not user_info: return redirect('/login/') def process_view(self, request, callback, callback_args, callback_kwargs): pass def process_exception(self, request, exception): #捕捉所有异常,使其不会出现报错页面 #异常写入日志 return HttpResponse('服务器出问题了...') def process_response(self, request, response): print('m1.process_response') return response def process_template_response(self, request, response): #视图函数返回的对象,如果有render方法,则该方法就会被执行 return
from django.shortcuts import render, redirect, HttpResponse from app01.forms import LoginForm from app01 import models from django.conf import settings from utils.md5 import md5 def auth(func): def inner(request,*args,**kwargs): user_info = request.session.get(settings.USER_SEESION_KEY) if not user_info: return redirect('/login/') res = func(*args,**kwargs) return res return inner def login(request): if request.method == 'GET': form=LoginForm() return render(request,'login.html',{'form':form}) else: form = LoginForm(request.POST) if form.is_valid(): form.cleaned_data['password'] = md5(form.cleaned_data['password']) user = models.UserInfo.objects.filter(**form.cleaned_data).first() if user: request.session[settings.USER_SEESION_KEY] = {'id':user.pk,'username':user.username} return redirect('/index/') else: form.add_error('password','用户名或密码错误') return render(request,'login.html',{'form':form}) class Foo(object): def __init__(self): pass def show(self): pass def render(self): self.show() return HttpResponse('有render方法') def index(request): return Foo() def host(request): return HttpResponse('主机管理') def testt(request): print('views.test') return HttpResponse('...')