Deploying a Django Application to Google App Engine



1. Make your app run locally

2. Change to a Cloud SQL server & run it locally via a proxy

gcloud sql instances describe [YOUR_INSTANCE_NAME]
wget -O
chmod +x cloud_sql_proxy
./cloud_sql_proxy \
2018/11/16 13:52:35 Listening on for [connectionName]
2018/11/16 13:52:35 Ready for new connections

3. Modify so Django can talk to the new database

# [START db_setup]
if os.getenv('GAE_APPLICATION', None):
# Running on production App Engine, so connect to Google Cloud SQL using
# the unix socket at /cloudsql/<your-cloudsql-connection string>
'default': {
'ENGINE': 'django.db.backends.mysql',
# Running locally so connect to either a local MySQL instance or connect
# to Cloud SQL via the proxy. To start the proxy via command line:
# $ cloud_sql_proxy -instances=[INSTANCE_CONNECTION_NAME]=tcp:3306
# See
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': '',
'PORT': '3306',
# [END db_setup]
python migrate

4. Add other necessary files/requirements to your app


# [START django_app]
runtime: python37
# This configures Google App Engine to serve the files in the app's
# static directory.
- url: /static
static_dir: static/
# This handler routes all requests not caught above to the main app.
# It is required when static routes are defined, but can be omitted
# (along with the entire handlers section) when there are no static
# files defined.
- url: /.*
script: auto
# [END django_app]


from mysite.wsgi import application# App Engine by default looks for a file at the root of the app
# directory with a WSGI-compatible object called app.
# This file imports the WSGI-compatible object of the Django app,
# application from mysite/ and renames it app so it is
# discoverable by App Engine without additional configuration.
# Alternatively, you can add a custom entrypoint field in your app.yaml:
# entrypoint: gunicorn -b :$PORT mysite.wsgi
app = application


$ pip freeze > requirements.txt


# Static files (CSS, JavaScript, Images)
# Google App Engine: set static root for local static files
STATIC_URL = '/static/'
STATIC_ROOT = 'static'


WSGI config for mysite project.
It exposes the WSGI callable as a module-level variable named ``application``.For more information on this file, see
import osfrom django.core.wsgi import get_wsgi_applicationos.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')application = get_wsgi_application()

5. Gather Static Files

python collectstatic

6. Deploy your app

gcloud app deploy


Top writer in Technology | Backend Web Developer |

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store