I'm using the login_required
decorator and another decorator which paginates output data. Is it important which one comes first?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
According to PEP 318 the syntax for function decorators is:
this is equivalent to:
and dec1 is called before dec2.
You can define these functions to check like this:
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 thatlogin_required
function generates an abortBest implementation for login_required decorator in flask is:
according the implement of login_required,
You should do it like below.
suppose you have another decorator is_admin to judge a user have admin permission, you should do it like below
The Flask documentation specifies that the order matters if the function is a view and has a
route
decorator. From the docs: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.