我的第一个 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
\n\n\nAssertionError:视图函数映射正在覆盖现有端点函数:index
\n
此错误表明您在多个路由中使用了相同的方法名称。
\n\n您可以使用以下代码重现该错误:
\n\nfrom 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)\nRun 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\nRun Code Online (Sandbox Code Playgroud)\n\n两条路线@app.route("/info")都@app.route("/")使用相同的方法,称为index. 所以我收到错误:AssertionError: View function mapping is overwriting an existing endpoint function: index
我想,你也犯了同样的错误。您正在使用和路线index的方法。//information
| 归档时间: |
|
| 查看次数: |
6131 次 |
| 最近记录: |