django系列8.5--使用装饰器(视图函数中)实现用户登录状态检验

views.py

def session_auth(fn):
    def inner(request,*args,**kwargs):
        status = request.session.get('session_login')
        if status:
            ret = fn(request)
            return ret
        else:
            return redirect(reverse('login'))
    return inner


@session_auth
def index(request):
    username = request.session['user']
    # print('index')
    return render(request, 'index.html',{'username':username})

  

posted @ 2019-03-08 19:41  robertx  阅读(246)  评论(0编辑  收藏  举报