Search code examples
google-app-enginepython-2.7webapp2

App Engine can't find static files


I'm using webapp2 and python 2.7 on my app engine application. However, I'm having a problem with the url on app.yaml.

I have my static files inside a static/ directory, in the root of my path. So inside of static/ I have static/scripts/MyScript.js

When I set app.yaml like:

application: myapp
version: 1
runtime: python27
threadsafe: true
api_version: 1

handlers:
- url: /.*
  script: myapp.app

- url: /static
  static_dir: static

libraries:
- name: webapp2
  version: latest

In my HTML code, the js is called like: <script src="static/scripts/Fuel.js"></script>

But, the file is not loaded, and I get a 404 error. (This problem happens also for .css files.)

However if I change the first url in app.yaml to:

handlers:
- url: /
  script: myapp.app

The static files are loaded, but when I try calling the routes url in my app, like an url I call in a form to save some data on server, this route is not found and I also get a 404 error.

Here is my routing code in myapp.py file.

app = webapp2.WSGIApplication(
                              [('/', MainHandler),
                              ('/save', SaveData),], 
                              debug=True)

So if I try to access myapp.appspot.com/save, it returns me a 404 error, saying:

The requested URL /save was not found on this server.

Any ideas? If you need more information about it, just ask on the comments.

Thanks.


Solution

  • Put your catch all url /.* at the bottom since this is evaluated in order. Meaning it never pass after that.

    handlers:
    - url: /static
      static_dir: static
    
    - url: /.*
      script: myapp.app