from django import views from django.shortcuts import render,HttpResponse,redirect from django.views import View from functools import wraps #cbv必须要倒的模块 from django.utils.decorators import method_decorator # Create your views here. def login(request): if request.method == 'POST': username = request.POST.get('username') password = request.POST.get('password') if username == 'hkj' and password == '123': request.session['name']='hkj' request.session['password']='123' return HttpResponse('ok') else: return render(request,'login.html') def outer(func): @wraps(func) def inner(*args,**kwargs): if args[0].session.get('name'): return func(*args,**kwargs) else: redirect('/login') return inner #单独装饰一个 @method_decorator(outer,name='get') class A(View): # 拦截父类的函数分发 直接装饰分发函数 get 和post都被装饰 @method_decorator(outer) def dispatch(self, request, *args, **kwargs): super().dispatch(request,*args,**kwargs) def get(self,request): return HttpResponse('get') def post(self,request): return HttpResponse('post')