close

[Solved] AssertionError: View function mapping is overwriting an existing endpoint function: main

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error AssertionError: View function mapping is overwriting an existing endpoint function: main in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How AssertionError: View function mapping is overwriting an existing endpoint function: main Error Occurs?

Today I get the following error AssertionError: View function mapping is overwriting an existing endpoint function: main in python.

How To Solve AssertionError: View function mapping is overwriting an existing endpoint function: main Error ?

  1. How To Solve AssertionError: View function mapping is overwriting an existing endpoint function: main Error ?

    To Solve AssertionError: View function mapping is overwriting an existing endpoint function: main Error Your view names need to be unique even if they are pointing to the same view method.

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

    To Solve AssertionError: View function mapping is overwriting an existing endpoint function: main Error Your view names need to be unique even if they are pointing to the same view method.

Solution 1

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'])

Solution 2

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

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read