Route to view_func with the same flask decorators

suggests that I have these 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 the same @validate_access decorator. 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 do not know if there is his mistake or not. But please let me know if there is a solution for this.

Thanks:)

+3
python flask decorator
source share
1 answer

If you do not provide endpoint before add_url_rule or route , the method name will be used as the endpoint. What happens is a rule is created with the name of your wrapper function, and not with a decorated function, probably because you are not using functools.wraps

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

All Articles