I have a flask app, where i have implemented a snippet, to check if a user is logged in, in order to acces certain webpages on my application.
My method looks like this:
#check if session is avaliable to access hidden pages for non users
def is_logged_in(f):
@wraps(f)
def wrap(*args, **kwargs):
if 'logged_in' in session:
return f(*args, **kwargs)
else:
flash('Please Login ', 'danger')
return redirect(url_for('login'))
return wrap
Here I check if the session has a logged_in
attribute attached the the session.
However, I get an error saying global name @wraps is not defined
,
but I have no idea as to why?
wraps() is a decorator that is applied to the wrapper function of a decorator. It updates the wrapper function to look like wrapped function by copying attributes such as __name__, __doc__ (the docstring), etc. Parameters: wrapped: The function name that is to be decorated by wrapper function.
functools. wraps is convenience function for invoking update_wrapper() as a function decorator, when defining a wrapper function. It is equivalent to partial(update_wrapper, wrapped=wrapped, assigned=assigned, updated=updated). So @wraps decorator actually gives a call to functools. partial(func[,*args][, **keywords]).
Understanding Flask decorators A decorator is a function that takes in another function as a parameter and then returns a function. This is possible because Python gives functions special status. A function can be used as a parameter and a return value, while also being assigned to a variable.
you are probably missing wraps from functools
from functools import wraps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With