View function mapping is overwriting an existing endpoint function: (endpoint)

Raise code
elf.url_rule_class(rule, methods=methods, **options)
rule.provide_automatic_options = provide_automatic_options # type: ignore
self.url_map.add(rule)
if view_func is not None:
old_func = self.view_functions.get(endpoint)
if old_func is not None and old_func != view_func:
raise AssertionError(
"View function mapping is overwriting an existing"
f" endpoint function: {endpoint}"
)
self.view_functions[endpoint] = view_func
@setupmethod
def template_filter(self, name: t.Optional[str] = None) -> t.Callable:
"""A dec """
Links to the raise (1)
https://github.com/pallets/flask/blob/83f7efa04708a22c9701488f0f29b59c2b756bef/src/flask/app.py#L1085Ways to fix
This error is raised when two functions that have the same name are decorated with the route()
function of the Flask class. To make the explanation clearer let's jump over to the sample code with out further ado.
Steps to reproduce the error:
First install Flas in a virtual environment for easier reproducibility.
$ pipenv install Flask
$ pipenv shell
Then create a python file named flask.py (Or it can be any name as long it is a python file .i.e it has .py extension)
and write the following code in the file.
from flask import Flask
app = Flask(__name__)
@app.route('/hello')
def hello():
return 'Hellow world!'
@app.route('/hello2')
def hello():
return 'Hellow world! this is the second message'
# notice that both functions are named hello. this is the cause of the exception
if __name__ == '__main__':
app.run()
After that save the file and run it using
$ python3 main.py
You should see the following error output which means the exception has been reproduced correctly.
Traceback (most recent call last):
File "/Users/kelem/PycharmProjects/flask/main.py", line 11, in <module>
def hello():
File "/Users/kelem/PycharmProjects/flask/venv/lib/python3.9/site-packages/flask/scaffold.py", line 439, in decorator
self.add_url_rule(rule, endpoint, f, **options)
File "/Users/kelem/PycharmProjects/flask/venv/lib/python3.9/site-packages/flask/scaffold.py", line 57, in wrapper_func
return f(self, *args, **kwargs)
File "/Users/kelem/PycharmProjects/flask/venv/lib/python3.9/site-packages/flask/app.py", line 1090, in add_url_rule
raise AssertionError(
AssertionError: View function mapping is overwriting an existing endpoint function: hello
How to fix it:
Rename the second function in something other than hello. In our case we are going to name it hello2 like so.
from flask import Flask
app = Flask(__name__)
@app.route('/hello')
def hello():
return 'Hellow world!'
@app.route('/hello2')
def hello2():
return 'Hellow world! this is the second message'
if __name__ == '__main__':
app.run()
At this time if you run the file the same way as explained above it should work correctly. Go to your browser and type http://127.0.0.1:5000/hello and click return. You should see the following output.
If you go to the address http://127.0.0.1:5000/hello2 instead, you should see the message under hello2 printed out on the browser. I.e. Hellow
world! this is the second message