zoukankan      html  css  js  c++  java
  • Django自定义装饰器

    装饰器模板:

    def decorator(func):
        def wrapper(*args,**kwargs):
            return func(*args,**kwargs)  
        return wrapper
    

      

    装饰器应用:

    def log_in(func):
        '''身份认证装饰器,
        :param func:
        :return:
        '''
        def wrapper(request,*args,**kwargs):
            if not request.session.get("is_login"):
                return redirect("/book/login/")
            return  func(request,*args, **kwargs)
        return wrapper
    
    @log_in #author=log_in(author) 
    def author(request):
        author_list = models.Author.objects.all()
        return render(request, "author.html", {"authorList": author_list})
    

      

    from django.contrib.auth.decorators import login_required
    from django.utils.decorators import method_decorator
    from django.views.generic import TemplateView
    
    class ProtectedView(TemplateView):
        template_name = 'secret.html'
    
        @method_decorator(login_required)
        def dispatch(self, *args, **kwargs):
            return super(ProtectedView, self).dispatch(*args, **kwargs)
    

      

  • 相关阅读:
    Linux
    前端
    第一章 初识 MyBatis
    mysql 复习
    五 、 Kafka producer 拦截器(interceptor) 和 六 、Kafka Streaming案例
    spark graphx图计算
    四、Kafka API 实战
    三、Kafka工作流程分析
    二、Kafka集群部署
    一、KafKa概述
  • 原文地址:https://www.cnblogs.com/pythonClub/p/9864603.html
Copyright © 2011-2022 走看看