我有一個名爲裝飾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裝飾器中調用什麼?