AssertionError: View function mapping is overwriting an existing endpoint function: main
Does anyone know why I can't overwrite an existing endpoint function if i have two url rules like this
app.add_url_rule('/',
view_func=Main.as_view('main'),
methods=["GET"])
app.add_url_rule('/<page>/',
view_func=Main.as_view('main'),
methods=["GET"])
Traceback:
Traceback (most recent call last):
File "demo.py", line 20, in <module> methods=["GET"])
File ".../python2.6/site-packages/flask/app.py",
line 62, in wrapper_func return f(self, *args, **kwargs)
File ".../python2.6/site-packages/flask/app.py",
line 984, in add_url_rule 'existing endpoint function: %s' % endpoint)
AssertionError: View function mapping is overwriting an existing endpoint
function: main
This same issue happened to me when I had more than one API function in the module and tried to wrap each function with 2 decorators:
- @app.route()
- My custom @exception_handler decorator
I got this same exception because I tried to wrap more than one function with those two decorators:
@app.route("/path1")
@exception_handler
def func1():
pass
@app.route("/path2")
@exception_handler
def func2():
pass
Specifically, it is caused by trying to register a few functions with the name wrapper:
def exception_handler(func):
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as e:
error_code = getattr(e, "code", 500)
logger.exception("Service exception: %s", e)
r = dict_to_json({"message": e.message, "matches": e.message, "error_code": error_code})
return Response(r, status=error_code, mimetype='application/json')
return wrapper
Changing the name of the function solved it for me (wrapper.__name__ = func.__name__):
def exception_handler(func):
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as e:
error_code = getattr(e, "code", 500)
logger.exception("Service exception: %s", e)
r = dict_to_json({"message": e.message, "matches": e.message, "error_code": error_code})
return Response(r, status=error_code, mimetype='application/json')
# Renaming the function name:
wrapper.__name__ = func.__name__
return wrapper
Then, decorating more than one endpoint worked.
Your view names need to be unique even if they are pointing to the same view method.
app.add_url_rule('/',
view_func=Main.as_view('main'),
methods = ['GET'])
app.add_url_rule('/<page>/',
view_func=Main.as_view('page'),
methods = ['GET'])
For users that use @app.route it is better to use the key-argument endpoint
rather then chaning the value of __name__
like Roei Bahumi stated. Taking his example will be:
@app.route("/path1", endpoint='func1')
@exception_handler
def func1():
pass
@app.route("/path2", endpoint='func2')
@exception_handler
def func2():
pass
Flask requires you to associate a single 'view function' with an 'endpoint'. You are calling Main.as_view('main')
twice which creates two different functions (exactly the same functionality but different in memory signature). Short story, you should simply do
main_view_func = Main.as_view('main')
app.add_url_rule('/',
view_func=main_view_func,
methods=["GET"])
app.add_url_rule('/<page>/',
view_func=main_view_func,
methods=["GET"])
This can happen also when you have identical function names on different routes.