简体   繁体   中英

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. 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).

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,

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

In the html to link the stylesheet. My app.yaml file is very basic:

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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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