简体   繁体   English

Google App Engine无法提供静态文件

[英]Google App Engine not serving static files

I realize that this has been asked a couple of times, but none of the solutions seemed to help. 我意识到这已被问过几次,但没有一个解决方案似乎有所帮助。

I am trying to use Flask to serve some basic HTML and CSS to my website. 我正在尝试使用Flask为我的网站提供一些基本的HTML和CSS。 On App Engine, Flask is serving the template correctly, but is not able to locate the stylesheet in the static/css folder (I'm getting 404s in my logs on the developer console). 在App Engine上,Flask正确地提供模板,但是无法在static / css文件夹中找到样式表(我在开发人员控制台的日志中获得了404s)。

My project's relevant structure is this: 我的项目的相关结构是这样的:

/app
    /static
        /css
            style.css
    /templates
        home.html

The commands I'm using are: 我正在使用的命令是:

render_template('home.html')

In the main python file, 在主python文件中,

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

In the html to link the stylesheet. 在html中链接样式表。 My app.yaml file is very basic: 我的app.yaml文件非常基本:

runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /.*
  script: main.app
- url: /static
  static_dir: static
  expiration: "7d"

Now, this works fine when I run it on localhost, and the url_for function is getting the stylesheet correctly, but not on Google Cloud Platform, despite getting the correct filepath and having the correct source code in the Development tab, only the HTML is served. 现在,当我在localhost上运行它时,这种方法正常,并且url_for函数正确地获取样式表,但在Google Cloud Platform上没有,尽管获得了正确的文件路径并且在“开发”选项卡中有正确的源代码,但只提供了HTML 。 If it helps, this is the command I'm using to deploy the app: 如果有帮助,这是我用来部署应用程序的命令:

 gcloud app deploy app.yaml --project my-project

I'm sure the problem is right in front of me, but I can't figure out. 我确定问题就在我面前,但我无法弄明白。 Any insight would be appreciated. 任何见解将不胜感激。 Thank you in advance. 先感谢您。

The order of the handlers in the app.yaml file matters, your /static one will never be hit because it'll be matched by the /.* pattern first, you need to reverse their order. app.yaml文件中处理程序的顺序很重要,您的/static将永远不会被命中,因为它首先会与/.*模式匹配,您需要撤消它们的顺序。

The fact that it works on localhost could be because the static content is available together with the app code, but that's not true when deployed on GAE - static content by default goes elsewhere and is not accessible by app code. 它在localhost上工作的事实可能是因为静态内容可以与应用程序代码一起使用,但是当部署在GAE上时却不是这样 - 默认情况下静态内容转到其他地方并且应用程序代码无法访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM