Django个人博客搭建教程---在类中使用装饰器

urlpatterns = [
    url(r'^jiadmin/', views.JiaAdmin.as_view()),]

 # 为全部请求方法添加装饰器

@method_decorator(check_admin, name='dispatch')
class JiaAdmin(View):
    def get(self,request):
        return render(request,'jiaadmin.html')

# 为特定请求方法添加装饰器

@method_decorator(check_admin, name='get')
class JiaAdmin(View):
    def get(self,request):
        return render(request,'jiaadmin.html')

# 没有为put方法添加装饰器

class JiaAdmin(View):

    @method_decorator(check_admin)
    def get(self,request):
        return render(request,'jiaadmin.html')

    @method_decorator(check_admin)
    def post(self,request):
        return render(request, 'jiaadmin.html')

    def put(self,request):
        return render(request, 'jiaadmin.html')

 

你可能感兴趣的:(Django,django,装饰器)