Flask AssertionError:视图函数映射正在覆盖现有端点函数:index

1 python flask

我的第一个 hello world 程序无法运行。

这是我的代码:

from flask import Flask
app=Flask(__name__)
@app.route('/')
def index():
    return "<h1>Hello world</h1>"

if __name__=='__main__':
        app.run()
 
Run Code Online (Sandbox Code Playgroud)

这是我的输出:

(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)
Run Code Online (Sandbox Code Playgroud)

AssertionError:视图函数映射正在覆盖现有端点函数:index

ars*_*sho 8

\n

AssertionError:视图函数映射正在覆盖现有端点函数:index

\n
\n\n

此错误表明您在多个路由中使用了相同的方法名称。

\n\n

您可以使用以下代码重现该错误:

\n\n
from flask import Flask\n\napp = Flask(__name__)\n\n@app.route("/")\ndef index():\n    return "hello from index"\n\n@app.route("/info")\ndef index():\n    return "hello from info"\n\napp.run(debug=True, port=8080)\n
Run Code Online (Sandbox Code Playgroud)\n\n

错误跟踪:

\n\n
(venv) \xe2\x9e\x9c python app.py \nTraceback (most recent call last):\n  File "app.py", line 9, in <module>\n    @app.route("/info", methods=[\'GET\'])\n  File ".../app.py", line 1250, in decorator\n    self.add_url_rule(rule, endpoint, f, **options)\n  File ".../app.py", line 66, in wrapper_func\n    return f(self, *args, **kwargs)\n  File ".../app.py", line 1221, in add_url_rule\n    \'existing endpoint function: %s\' % endpoint)\nAssertionError: View function mapping is overwriting an existing endpoint function: index\n
Run Code Online (Sandbox Code Playgroud)\n\n

两条路线@app.route("/info")@app.route("/")使用相同的方法,称为index. 所以我收到错误:AssertionError: View function mapping is overwriting an existing endpoint function: index

\n\n

我想,你也犯了同样的错误。您正在使用和路线index的方法。//information

\n