Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AssertionError: View function mapping is overwriting an existing endpoint function: main

Tags:

python

flask

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 
like image 545
Kimmy Avatar asked Jun 23 '13 00:06

Kimmy


2 Answers

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']) 
like image 42
Michael Davis Avatar answered Oct 02 '22 17:10

Michael Davis


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:

  1. @app.route()
  2. 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.

like image 131
Roei Bahumi Avatar answered Oct 02 '22 17:10

Roei Bahumi