使用 Flask App 在本地机器上出现 404 错误

404 Error on local machine with Flask App

首先让我说我熟悉 Python 作为数据工具,但这是我第一次使用 Python/Flask...我正在通过 Flask 工作Mega Tutorial book - 并决定尝试为公司销售团队制作仪表板。我才刚刚开始,我不知道自己做错了什么。

Here 是我在 Github 上的项目的 link 我认为这是因为导入是循环的?但我不确定如何解决它。我的 app.py 文件创建了应用程序,然后它被导入到 routes.py 和 models.py。 routes.py 依赖于我的用户 class 的 models.py。我可以 运行 它在本地,它说 Flask 应用程序正在 运行ning 但我在浏览器中收到 404 错误。

if __name__ == "__main__":
    app.run(debug=True)

应该放在routes.py的末尾,而不是app.py.

正在阅读此 answer

When the Python interpreter reads a source file, it executes all of the code found in it.

Before executing the code, it will define a few special variables. For example, if the Python interpreter is running that module (the source file) as the main program, it sets the special __name__ variable to have a value __main__. If this file is being imported from another module, __name__ will be set to the module's name.

所以上面的代码只会在你 运行 你的 app.py 时执行。如果你 运行 routes.py 它将变得毫无意义,除非它被放置在 routes.py 模块中。

我测试了它,它按预期工作: