Does decorators on a flask justify the order?

I use the login_required decorator and another decorator that splits the output. Is it important that someone comes first?

+6
source share
2 answers

Despite the fact that in this case there will probably not be any problems, no matter what order, you probably want login_required to login_required executed first so that you don't make paginate queries and results that just get deleted.

Decorators are applied from top to bottom, so login_required should be closest to the def line.


The real answer is that it depends on what each of the decorators do. You need to think about the flow of your program and whether it makes sense for someone to come before another.

+6
source

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 this way:

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

This does not actually make any mistake, but if you use login_reqired and the user has not registered in the application, he will process the data and paginate it after that. login_required function generates an interrupt

Best implementation for login_required decoder in bulb:

 @paginate @login_required def view_function(): pass 
+2
source

Source: https://habr.com/ru/post/981691/


All Articles