小编典典

Flask classful 为最小应用程序提供 404 错误

all

我正在阅读这份文件:https ://flask-classful.teracy.org/

从上面的文档中复制了最小的应用程序。

from flask import Flask
from flask_classful import FlaskView

# we'll make a list to hold some quotes for our app
quotes = [
    "A noble spirit embiggens the smallest man! ~ Jebediah Springfield",
    "If there is a way to do it better... find it. ~ Thomas Edison",
    "No one knows what he can do till he tries. ~ Publilius Syrus"
]

app = Flask(__name__)

class QuotesView(FlaskView):
    @app.route("/")
    def index(self):
        return "<br>".join(quotes)

QuotesView.register(app)

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

当我运行这个应用程序时,它给出了 404 错误。

(venv) C:\fifa\web_app_flask_bootstrap>python app.py
 * Serving Flask app 'app' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000 (Press CTRL+C to quit)
127.0.0.1 - - [28/Jun/2022 13:44:56] "GET / HTTP/1.1" 404 -

我正在安装以下烧瓶相关的软件包:

Flask==2.1.2
Flask-Classful==0.14.2
Python version: 3.9.2

显示该错误的原因可能是什么?

试用1:更改端口并尝试。app.run(port=5001)得到同样的错误。


阅读 92

收藏
2022-06-28

共1个答案

小编典典

@app.route('/')您提供的链接中的代码片段在函数之前不包含装饰器index。并且,在片段之后,它立即说:Run this app and open your web browser to: http://localhost:5000/quotes/.

我不知道它是如何Flask-Classful工作的,但我猜它采用类的名称并将其用作端点(在本例中QuotesView,将前面的所有内容设为View小写)。但是,这又是一个猜测。

2022-06-28