Skip to content Skip to sidebar Skip to footer

Python Flask - Setting A Cookie Using A Decorator

I'm trying to write a decorator that checks for a cookie, and sets one if it doesn't exist. This is my desperate attempt to get the idea across. def set_cookie(f): def decorate

Solution 1:

You have to call the original function:

def set_cookie(f):
    def decorated_function(*args, **kws):
        response = f(*args, **kws)
        response = make_response(response)
        if 'cstc' in flask.request.cookies.keys():
            response.set_cookie('cstc', value='value')
        return response
    return decorated_function

Solution 2:

from functools import wraps

def set_cookie(f):
    @wraps(f)
    def decorated_function(*args, **kws):
          #your code here
          return f(*args, **kws)
    return decorated_function

Post a Comment for "Python Flask - Setting A Cookie Using A Decorator"