Does the order of decorators matter on a Flask view? Does the order of decorators matter on a Flask view? flask flask

Does the order of decorators matter on a Flask view?


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.


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@paginatedef 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_admindef view_function():    pass


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.