Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'global name @wraps is not defined' error in flask [duplicate]

Tags:

python

flask

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?

like image 920
kristof Avatar asked Apr 05 '18 07:04

kristof


People also ask

What does @wraps do in Python?

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.

What is from Functools import wraps?

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]).

What is decorator in Flask?

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.


1 Answers

you are probably missing wraps from functools

from functools import wraps
like image 72
Krypotos Avatar answered Sep 22 '22 16:09

Krypotos