'Flask AssertionError: View function mapping is overwriting an existing endpoint function: index
My first hello world program is not working.
This is my code:
from flask import Flask
app=Flask(__name__)
@app.route('/')
def index():
return "<h1>Hello world</h1>"
if __name__=='__main__':
app.run()
And this is my output:
(myflaskenv) C:\Users\saini computers\Desktop\flask_examples>python
basic.py
Traceback (most recent call last):
File "basic.py", line 6, in <module>
@app.route('/information')
File "C:\Users\saini computers\Anaconda3\envs\myflaskenv\lib\site-
packages\flask\app.py", line 1250, in decorator
self.add_url_rule(rule, endpoint, f, **options)
File "C:\Users\saini computers\Anaconda3\envs\myflaskenv\lib\site-
packages\flask\app.py", line 66, in wrapper_func
return f(self, *args, **kwargs)
File "C:\Users\saini computers\Anaconda3\envs\myflaskenv\lib\site-
packages\flask\app.py", line 1221, in add_url_rule
'existing endpoint function: %s' % endpoint)
AssertionError: View function mapping is overwriting an existing endpoint function: index
Solution 1:[1]
AssertionError: View function mapping is overwriting an existing endpoint function: index
This error indicates that you have used same method name in multiple routes.
You can reproduce the error using the following code:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
return "hello from index"
@app.route("/info")
def index():
return "hello from info"
app.run(debug=True, port=8080)
Error trace:
(venv) ? python app.py
Traceback (most recent call last):
File "app.py", line 9, in <module>
@app.route("/info", methods=['GET'])
File ".../app.py", line 1250, in decorator
self.add_url_rule(rule, endpoint, f, **options)
File ".../app.py", line 66, in wrapper_func
return f(self, *args, **kwargs)
File ".../app.py", line 1221, in add_url_rule
'existing endpoint function: %s' % endpoint)
AssertionError: View function mapping is overwriting an existing endpoint function: index
Both routes @app.route("/info") and @app.route("/") use same method called index. So I am getting the error: AssertionError: View function mapping is overwriting an existing endpoint function: index
I think, you are making same mistake. You are using method index for both / and /information route.
Solution 2:[2]
As of version 4.x.x, it is now @jwt_required() instead of jwt_required. If you are updating an existing application, you can find a link to all of the breaking changes in the 4.x.x release on the README.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | arsho |
| Solution 2 | Karl Elias |
