Google App Engine无法提供静态文件

Not*_*ndo 4 python google-app-engine flask google-cloud-platform google-app-engine-python

我意识到这已被问过几次,但没有一个解决方案似乎有所帮助.

我正在尝试使用Flask为我的网站提供一些基本的HTML和CSS.在App Engine上,Flask正确地提供模板,但是无法在static/css文件夹中找到样式表(我在开发人员控制台的日志中获得404s).

我的项目的相关结构是这样的:

/app
    /static
        /css
            style.css
    /templates
        home.html
Run Code Online (Sandbox Code Playgroud)

我正在使用的命令是:

render_template('home.html')
Run Code Online (Sandbox Code Playgroud)

在主python文件中,

<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/style.css') }}">
Run Code Online (Sandbox Code Playgroud)

在html中链接样式表.我的app.yaml文件非常基本:

runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /.*
  script: main.app
- url: /static
  static_dir: static
  expiration: "7d"
Run Code Online (Sandbox Code Playgroud)

现在,当我在localhost上运行它时,这种方法正常,并且url_for函数正确地获取样式表,但在Google Cloud Platform上没有,尽管获得了正确的文件路径并且在"开发"选项卡中有正确的源代码,但只提供了HTML .如果有帮助,这是我用来部署应用程序的命令:

 gcloud app deploy app.yaml --project my-project
Run Code Online (Sandbox Code Playgroud)

我确定问题就在我面前,但我无法弄明白.任何见解将不胜感激.先感谢您.

Dan*_*scu 6

app.yaml文件中处理程序的顺序很重要,/static因为它首先会与/.*模式匹配,所以你永远不会被命中,你需要撤销它们的顺序.

它在localhost上工作的事实可能是因为静态内容可以与应用程序代码一起使用,但是当部署在GAE上时却不是这样 - 默认情况下静态内容转到其他地方并且应用程序代码无法访问.