This guide will walk you through the steps need to create and deploy a new Django application. It will then show you how you can modify the application and then redeploy it.
Before running this guide, you must install the Cloud SDK, Cloud SQL Proxy and setup your development environment.
-
Download and install the Cloud SDK
-
Download and install the Cloud SQL Proxy
-
Download and install Docker (any edition)
-
Prepare your environment for Python development. You have the Python 3.5 or later, pip and virtualenv installed on your system. For instructions, refer to the Python Development Environment Setup Guide.
-
If you don't already one, you need to create a Google Cloud Platform billing account. If you are new to Google Cloud Platform, you may be able to take advantage of a free trial.
When deploying your Django application, you can choose which Google serving technology to use:
A serverless deployment environment that requires minimal configuration and no server maintenance [learn more].
A managed container environment that allows extensive customization of the application's execution environment [learn more].
Create a development environment and install Django Deploy into it.
-
Create a new virtual environment to run Django Deploy:
virtualenv -p python3 django-deploy
-
Activate the new virtual environment:
source django-deploy/bin/activate
-
Install Django Deploy
pip install django-cloud-deploy
-
Start Django Deploy to create and deploy a new Django application.
When using App Engine:
django-cloud-deploy new --backend=gae
When using Google Kubernetes Engine:
django-cloud-deploy new --backend=gke
After running one of the above commands, you should see:
11 steps to setup your new project ...
-
Follow the prompts displayed in the terminal. Make sure that you remember the following information:
- the database password for the default user
- the directory location of the project source
- the username and password for the Django admin site
-
Once you have answered all of the prompts, your new Django project will be created and deployed.
At the end of the process, you will see:
Your app is running at <url>
-
Open
<url>
in your browser to see your application running.
You can test your application locally using the built-in Django development server.
-
Change into your project's root directory (the one containing the file
manage.py
):cd <project root directory>
-
Apply the needed database migrations:
python manage.py migrate
-
Start the local development server with the following command:
python manage.py runserver
-
View the site running on your computer at http://127.0.0.1:8000/. You should see this text in your browser:
Hello from the Cloud
You can leave the development server running while you develop your application. The development server reloads your code as needed so you don't need to restart it for your changes to take effect. Note that not all changes will be detected (for example adding a new file), so you should restart the development server if your changes don't appear.
-
Try it now: Leave the development server running, then open
<app name>/views.py
. You should see code like this:def index(request): return HttpResponse("<h1>Hello from the Cloud!</h1>")
-
Change
"<h1>Hello from the Cloud!</h1>"
to some HTML of your choosing. -
Reload http://127.0.0.1:8000/ to see the results.
When you are done testing your code locally, you can redeploy to the cloud.
-
Start Django Deploy to update your application:
django-cloud-deploy update
After running the above commands, you should see:
3 steps to update your new project ...
-
Follow the prompts displayed in the terminal.
-
Once you have answered all of the prompts, your Django project will be updated.
At the end of the process, you will see:
Your app is running at <url>
-
Open
<url>
in your browser to see your updated application running.
To avoid incurring charges to your GCP account for the resources used in this guide:
-
In the GCP Console, go to the Projects page.
-
In the project list, select the project you want to delete and click Delete.
-
In the dialog, type the project ID, and then click Shut down to delete the project.