gpt4 book ai didi

python - Flask:无法访问静态文件夹中的文件(404)

转载 作者:行者123 更新时间:2023-12-01 02:42:02 30 4
gpt4 key购买 nike

我想用 Flask 提供一个 React 应用程序,但“static_folder”似乎不起作用。这是我的最小代码:

from flask import Flask
from flask_restful import Resource, Api

app = Flask(__name__, static_folder="react_app/build")
api = Api(app)

class HelloWorld(Resource):
def get(self):
return app.send_static_file("index.html")

api.add_resource(HelloWorld, '/')

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

在浏览器中我可以访问 http://localhost:5000 它返回我正确的index.html。但是所有静态文件(例如 css 和 js 文件)都会返回 404 错误。例如,有一个文件:react_app/build/bundle.cssreact_app/build/js/some_name.js。但输入 http://localhost:5000/bundle.css http://localhost:5000/js/some_name.js 都返回 404 未找到错误。

据我了解,如果我指定 static_folder,我应该从根 url 访问该文件夹中的所有文件。我错过了什么/误解了什么?

最佳答案

不,那不是 the documentation说:

  • static_url_path – can be used to specify a different path for the static files on the web. Defaults to the name of the static_folder folder.
  • static_folder – the folder with static files that should be served at static_url_path. Defaults to the 'static' folder in the root path of the application.

因此,您的文件将在 localhost:5000/react_app/build 上提供。由于这可能不是您想要的,因此您还应该传递static_url_path,其值类似于“static”,以便您可以在 localhost:5000/static 找到文件。

关于python - Flask:无法访问静态文件夹中的文件(404),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45591749/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com