GAE + Django 3.0 production-ready app using cookiecutter in 5 minutes

Adrián Castellanos
bluekiri
Published in
5 min readApr 24, 2020

The objective of this tutorial is to build a production-ready Django app that runs on App Engine standard environment and scales dynamically according to traffic.

This tutorial assumes that you’re familiar with the Django framework. If you’re new to Django, it’s a good idea to work through writing your first Django app before continuing.

This tutorial requires Python 3.7.

This tutorial is divided into two parts:

  • Create an infrastructure in Google Cloud Platform
  • Use a cookie-cutter to create a Django project with the settings of the Google Cloud Platform infrastructure

Google Cloud Platform setup

We will create a new project for this tutorial. The name of the project will be “gae-demo”.

The services that we are going to create will be saved under this project. Now we need to create a Google App Engine instance and a PostgreSQL instance.

Google App Engine setup

We are going to start an App Engine instance running Python and a standard environment. The standard environments will use Python 3.7 to run our code.

PostgreSQL setup

We will create a relational database server. We will be asked to select a database engine. Select PostgreSQL.

At this step, remember to configure the backups and change the resources of the machine if needed.

Once the database server is created, we will need to create a database inside the database server.

We will also need to create a username with a password in order to give access to the Django app to the database.

The settings of the database that we just created are:

  • Database name: gae-demo
  • Database user: admin
  • Database password: admin
  • Instance connection name: affable-hall-275208:us-central1:gae-demo-db

The Instance connection name can be found in the SQL dashboard:

Django setup

To start the Django project, we need to create a Python virtual environment to run our code. For this tutorial, we will be using Python 3.7 and Django 3.0.

$ python3 -m venv gaevenv
$ source gaevenv/bin/activate

Once the virtual environment is created we will use a cookiecutter to build the code. Cookiecutter is a CLI tool (Command Line Interface) to create an application boilerplate from a template. For this tutorial, we will be using the following template from GitHub.

To start the project using the template we will install the cookie-cutter CLI and we will fill a form with the information of our project. Note that the data that we are asked to fill, should match the data from the Google Cloud Platform.

$(gaevenv) pip install cookiecutter
$(gaevenv) cookiecutter gh:https://github.com/adriancast/GAE-django-cookiecutter

Once the project is created, remember to install the requirements running:

$(gaevenv) pip install -r requirements.txt

Google Cloud Platform CLI

Before deploying to production we need to install the Google Cloud SDK. This CLI will be used later for the deployment. Once the SDK is installed, you can log in using your Google credentials

$(gaevenv) gcloud auth application-default login

Create Django tables in the production database

The database server that we created is in a private network. This means that we cannot connect to the database from our local machine without using a proxy.

Google provides a proxy tool to perform this task. Note that you will need to run this script in the background every time that you want to connect from your local machine to the production database. You can get the tool from this Google tutorial.

In the bottom of the Django settings, there is a guide of the command that you need to execute to connect to the production database using the proxy tool.

Note that the “instances” argument from the cloud_sql_proxy is being dynamically generated using the information given in the project template form.

Using the example from above, in order to connect to the production database we will use the proxy tool and then run the “migrate” command from the manage.py.

$(gaevenv) ./cloud_sql_proxy -instances=”affable-hall-275208:us-central1:gae-demo-db”=tcp:3306
$(gaevenv) python manage.py migrate

Verify production environment

Once the production database is configured, we can deploy the app to production executing the following command:

$(gaevenv) gcloud app deploy

You can stream logs from the command line by running:

$(gaevenv) gcloud app logs tail -s default

To view your application in the web browser run the following command:

$(gaevenv) gcloud app browse

To check if everything is ok, try the /admin route. If you get the Django admin login, you finished this tutorial successfully.

--

--

Adrián Castellanos
bluekiri

Full stack developer @logitravelgroup. Pythonista 🐍