from: 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})