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
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'])
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With