首页 > 其他 > 详细

CBV 验证装饰器的使用

时间:2017-03-10 12:31:18      阅读:113      评论:0      收藏:0      [点我收藏+]

下面是3种方式: 

技术分享

from django.shortcuts import render, redirect
from django.views import View
# Create your views here.


class Login(View):

    def get(self, request):
        return render(request, ‘login.html‘)


def check_login(func):
    def inner(request, *args, **kwargs):
        if request.session.get(‘user_info‘):
            return func(request, *args, **kwargs)
        else:
            return redirect(‘/login.html‘)
    return inner

from django.utils.decorators import method_decorator

@method_decorator(check_login, name=‘dispatch‘)
class Index(View):

    @method_decorator(check_login)
    def dispatch(self, request, *args, **kwargs):
        return super(Index,self).dispatch( request, *args, **kwargs)

    @method_decorator(check_login)
    def get(self, request):
        return render(request, ‘index.html‘)

    def post(self, request):
        return render(request, ‘index.html‘)

  

CBV 验证装饰器的使用

原文:http://www.cnblogs.com/wumingxiaoyao/p/6529963.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!