• CBV登录装饰器


    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')
  • 相关阅读:
    Docker安装ngnix进行挂载
    Linux上传下载小工具
    uniapp——原生导航栏
    uniapp——scroll-view组件隐藏滚动条
    边框阴影——box-shadow
    uniapp-监听自定义跳转
    uniapp整屏滑动
    用伪类写圆点css
    Vue——生命周期
    uniapp多选按钮
  • 原文地址:https://www.cnblogs.com/huikejie/p/11217903.html
Copyright © 2020-2023  润新知