Search code examples
pythonpython-2.7flaskdecorator

Route to view_func with same decorators "flask"


lets suppose i have this routes:

app.add_url_rule('/',
                  view_func=index,
                  methods=['GET'])

app.add_url_rule('login',
                  view_func=login,
                  methods=['GET', 'POST'])

@validate_access()
def index():
    #......

@validate_access()
def login():
    #......

I have 2 endpoints with same decorator "@validate_access". When i run this code i got

AssertionError: View function mapping is overwriting an existing endpoint function:    wrapperAssertionError: View function mapping is overwriting an existing endpoint function: wrapper

I don't know if its a bug or not. But please inform me if there is a solution for this.

Thanks :)


Solution

  • If you dont provide endpoint to add_url_rule or route, the name of the method will be used as the endpoint. What's happening is the rule is being created with the name of your wrapping function, rather than the decorated function, probably because you arent using functools.wraps

    from functools import wraps
    def my_decorator(f):
        @wraps(f)
        def wrapper(*args, **kwds):
            return f(*args, **kwds)
        return wrapper