Help with authorization and redirection decorator in python (pylons)
- by ensnare
I'm trying to write a simple decorator to check the authentication of a user, and to redirect to the login page if s/he is not authenticated:
def authenticate(f):
try:
if user['authenticated'] is True:
return f
except:
redirect_to(controller='login', action='index')
class IndexController(BaseController):
@authenticate
def index(self):
return render('/index.mako' )
But this approach doesn't work. When a user is authenticated, everything is fine. But when the user is not authenticated, redirect_to() doesn't work and I am given this error:
HTTPFound: 302 Found Content-Type: text/html; charset=UTF-8 Content-Length: 0 location: /login
Thank for your help!