Does the order of decorators matter on a Flask view?

according the implement of login_required,

def login_required(func):
    @wraps(func)
    def decorated_view(*args, **kwargs):
        if current_app.login_manager._login_disabled:
            return func(*args, **kwargs)
        elif not current_user.is_authenticated():
            return current_app.login_manager.unauthorized()
        return func(*args, **kwargs)
    return decorated_view

You should do it like below.

@login_required
@paginate
def view_function():
    pass

suppose you have another decorator is_admin to judge a user have admin permission, you should do it like below

@login_required
@is_admin
def view_function():
    pass

While there probably won't be any problem in this case no matter what the order, you probably want login_required to execute first so that you don't make queries and paginate results that will just get thrown away.

Decorators wrap the original function bottom to top, so when the function is called the wrapper added by each decorator executes top to bottom. @login_required should be below any other decorators that assume the user is logged in so that its condition is evaluated before those others.

@app.route() must always be the top, outermost decorator. Otherwise the route will be registered for a function that does not represent all the decorators.


The broader answer is that it depends on what each of the decorators are doing. You need to think about the flow of your program and whether it would make logical sense for one to come before the other.


The Flask documentation specifies that the order matters if the function is a view and has a route decorator. From the docs:

When applying further decorators, always remember that the route() decorator is the outermost.


According to PEP 318 the syntax for function decorators is:

@dec2
@dec1
def func(arg1, arg2, ...):
    pass

this is equivalent to:

def func(arg1, arg2, ...):
    pass
func = dec2(dec1(func))

and dec1 is called before dec2.

You can define these functions to check like this:

def dec1(func):
    print 'dec1'
def dec2(func):
    print 'dec2'

@dec2
@dec1
def func():
    pass
dec1
dec2

Actually it does not make any error but if you use login_reqired first and user is not logged in application will process data and paginate it after that login_required function generates an abort

Best implementation for login_required decorator in flask is:

@paginate
@login_required
def view_function():
    pass