Does the order of decorators matter on a Flask vie

2019-04-21 12:48发布

I'm using the login_required decorator and another decorator which paginates output data. Is it important which one comes first?

4条回答
做自己的国王
2楼-- · 2019-04-21 13:31

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
查看更多
▲ chillily
3楼-- · 2019-04-21 13:35

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
查看更多
不美不萌又怎样
4楼-- · 2019-04-21 13:35

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

To use the decorator, apply it as innermost decorator to a view function. When applying further decorators, always remember that the route() decorator is the outermost.

查看更多
啃猪蹄的小仙女
5楼-- · 2019-04-21 13:37

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. So login_required should be above any other decorators that assume the user is logged in.


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.

查看更多
登录 后发表回答