Serving Static Files

This part of the Python Guestbook code walkthrough shows how to serve static files. App Engine does not serve files directly out of your application's source directory unless configured to do so. But there are many cases where you want to serve static files directly to the web browser. Images, CSS stylesheets, JavaScript code, movies, and Flash animations are all typically stored with a web application and served directly to the browser.

This page is part of a multi-page tutorial. To start from the beginning and see instructions for setting up, go to Creating a Guestbook.

Configuring the app to use static files

The CSS files for the Guestbook app are in the bootstrap/css directory. The template for the app's web page, index.html, instructs the browser to load bootstrap.css and bootstrap-responsive.css, which are static files:

The app.yaml file specifies the bootstrap directory as the location for static files:

The handlers section defines two handlers for URLs. When App Engine receives a request for a URL beginning with /bootstrap, it maps the remainder of the path to files in the bootstrap directory, and if an appropriate file is found, the contents of the file are returned to the client. All other URLs match the /.* pattern, and are handled by the app object in the guestbook module.

URL path patterns are tested in the order they appear in app.yaml. In this case, the /bootstrap pattern matches before the /.* pattern for the appropriate paths. For more information on URL mapping and other options you can specify in app.yaml, see the app.yaml reference.

Send feedback about...