我有一个名为装饰Python的Bottle.py装饰混乱
def auth(check_func=validate_login):
def decorator(view):
def wrapper(*args, **kwargs):
auth = check_func()
if auth:
return view(*args, **kwargs)
return bottle.redirect('/login.html')
return wrapper
return decorator
的auth
装饰用这样
@get('/')
@view("someview")
@auth()
def handler():
#myhandlercode
“身份验证”,所以auth
装饰调用view
功能这使得我的模板在bottle.py中。 但现在我想返回json而不是呈现view
。那么我必须对auth
装饰器代码做出什么改变才能实现这一点?我很困惑如何从认证代码中调用处理程序,而不是view
。
编辑1:Bottle允许你返回字典,它直接将它转换成json。我不想使用视图,我只想从我的处理程序返回json给用户。所以我应该只删除@view装饰器?而我应该在auth装饰器中调用什么?