为什么我的flask hello world程序不起作用?

时间:2019-01-31 09:52:04

标签: flask

我的第一个hello world程序不起作用。

我是初学者。我不知道该怎么解决!!

这是我的代码:

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

if __name__=='__main__':
        app.run()

这是我的输出:

(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:视图函数映射正在覆盖现有端点       函数:索引

1 个答案:

答案 0 :(得分:1)

  

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

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

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

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)

错误跟踪:

(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

路由@app.route("/info")@app.route("/")都使用称为index的相同方法。所以我得到了错误:AssertionError: View function mapping is overwriting an existing endpoint function: index

我认为,您正在犯同样的错误。您正在对index/路由使用方法/information