Skip to content

Commit d14074a

Browse files
authored
Update Django Flex Example to CloudSQL v2 (#616)
* Update Django Flex Example to CloudSQL v2 * Remove env variable
1 parent facfc99 commit d14074a

File tree

4 files changed

+73
-55
lines changed

4 files changed

+73
-55
lines changed

appengine/flexible/django_cloudsql/README.md

Lines changed: 51 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,46 @@
1-
# Getting started with Django on Google Cloud Platform
1+
# Getting started with Django on Google Cloud Platform on App Engine Flexible
22

33
This repository is an example of how to run a [Django](https://www.djangoproject.com/)
4-
app on Google App Engine Flexible Environment. It uses the [Writing your first Django app](https://docs.djangoproject.com/en/1.9/intro/tutorial01/) as the example app to deploy.
4+
app on Google App Engine Flexible Environment. It uses the
5+
[Writing your first Django app](https://docs.djangoproject.com/en/1.9/intro/tutorial01/) as the
6+
example app to deploy.
57

68

79
## Setup the database
810

9-
This tutorial assumes you are setting up Django using a SQL database, which is the easiest way to run Django. If you have an existing SQL database running, you can use that, but if not, these are the instructions for creating a managed MySQL instance using CloudSQL.
11+
This tutorial assumes you are setting up Django using a SQL database, which is the easiest way to
12+
run Django.
1013

14+
* Create a [Second Generation CloudSQL instance](https://cloud.google.com/sql/docs/create-instance)
1115

12-
* Create a [CloudSQL instance](https://console.cloud.google.com/project/_/sql/create)
16+
* Ensure the [Cloud SQL Administration API](https://console.cloud.google.com/flows/enableapi?apiid=sqladmin) is enabled.
1317

14-
* In the instances list, click your Cloud SQL instance.
18+
* Install the [CloudSQL proxy](https://cloud.google.com/sql/docs/sql-proxy)
1519

16-
* Click Access Control.
17-
18-
* In the IP address subsection, click Request an IPv4 address to enable access to the Cloud SQL instance through an
19-
IPv4 address. It will take a moment to initialize the new IP address.
20-
21-
* Also under Access Control, in the Authorization subsection, under Allowed Networks, click the add (+) button .
20+
* Start the CloudSQL proxy using the connection string. The connection string can be obtained in the
21+
instance details in the console. It's in the form of <project>:<region>:<instance-name>.
22+
23+
./cloud_sql_proxy -instances=[INSTANCE_CONNECTION_NAME]=tcp:3306
2224

23-
* In the Networks field, enter 0.0.0.0/0. This value allows access by all IP addresses.
25+
* Create a root password
2426

25-
* Click Save.
27+
`gcloud sql instances set-root-password [YOUR_INSTANCE_NAME] --password [YOUR_INSTANCE_ROOT_PASSWORD]`
2628

27-
Note: setting allowed networks to 0.0.0.0/0 opens your SQL instance to traffic from any computer. For production databases, it's highly recommended to limit the authorized networks to only IP ranges that need access.
29+
* Use the root user and root password to create the `polls` database:
2830

29-
* Alternatively, the instance can be created with the gcloud command line tool as follows, substituting `your-root-pw
30-
with a strong, unique password.
31+
`mysql -h 127.0.0.1 -u root -p -e "CREATE DATABASE polls;"`
3132

32-
gcloud sql instances create <instance_name> --assign-ip --authorized-networks=0.0.0.0/0 set-root-password=your-root-pw
33+
* Edit `app.yaml` to change the `cloud_sql_instances` to reflect the connection name of the
34+
instance. This is in the form project:zone:instance
3335

34-
* Create a Database And User
36+
* Optionally, use the root account to create a new MySQL user.
3537

36-
* Using the root password created in the last step to create a new database, user, and password using your preferred MySQL client. Alternatively, follow these instructions to create the database and user from the console.
37-
* From the CloudSQL instance in the console, click New user.
38-
* Enter a username and password for the application. For example, name the user "pythonapp" and give it a randomly
39-
generated password.
40-
* Click Add.
41-
* Click Databases and then click New database.
42-
* For Name, enter the name of your database (for example, "polls"), and click Add.
38+
`mysql -h 127.0.0.1 -u root -p -e "CREATE USER 'user'@'%' IDENTIFIED BY 'password';" ``
39+
`mysql -h 127.0.0.1 -u root -p -e "GRANT ALL PRIVILEGES ON * . * TO 'user'@'%';" ``
4340

44-
Once you have a SQL host, configuring mysite/settings.py to point to your database. Change `your-database-name`,
45-
`your-database-user`, `your-database-host` , and `your-database-password` to match the settings created above. Note the
46-
instance name is not used in this configuration, and the host name is the IP address you created.
41+
Once you have a SQL host, configuring mysite/settings.py to point to your database. Change
42+
`your-cloudsql-connection-string` and `your-root-password`. If you created a new user and
43+
password, update those settings as well.
4744

4845
## Running locally
4946

@@ -55,6 +52,8 @@ contains just the Django dependency.
5552

5653
Once the database is setup, run the migrations.
5754

55+
python manage.py makemigrations
56+
python manage.py makemigrations polls
5857
python manage.py migrate
5958

6059
If you'd like to use the admin console, create a superuser.
@@ -65,14 +64,30 @@ The app can be run locally the same way as any other Django app.
6564

6665
python manage.py runserver
6766

68-
Now you can view the admin panel of your local site at http://localhost:8080/admin
67+
Now you can view the admin panel of your local site at http://localhost:8000/admin
6968

7069
## Deploying
7170

72-
Since the Django development server is not built for production, our container uses the Gunicorn server. Since Gunicorn doesn't serve static content,
73-
the static content is instead served from Google Cloud Storage.
71+
The app can be deployed by running
72+
73+
gcloud app deploy
74+
75+
You can view your site with:
76+
77+
gcloud app browse
78+
79+
Now you can view the admin panel of your deployed site at https://<your-app-id>.appspot.com/admin
80+
7481

75-
First, make a bucket and make it publically readable, replacing <your-gcs-bucket> with a bucket name, such as your project id:
82+
### Serving Static Files Using Cloud Storage
83+
84+
Since the Django development server is not built for production, our container uses the Gunicorn
85+
server. In `mysite/urls.py`, while in DEBUG mode, Django is configured to serve static files.
86+
However, in production, it's recommended to use Google Cloud Storage or an alternative CDN for
87+
serving static files.
88+
89+
First, make a bucket and make it publically readable, replacing <your-gcs-bucket> with a bucket name
90+
, such as your project id:
7691

7792
gsutil mb gs://<your-gcs-bucket>
7893
gsutil defacl set public-read gs://<your-gcs-bucket>
@@ -89,31 +104,17 @@ Now your static content can be served from the following URL:
89104

90105
http://storage.googleapis.com/<your-gcs-bucket/static/
91106

92-
Make sure to replace <your-cloud-bucket> within `mysite/settings.py` to set STATIC_URL to the correct value to serve static content from, and
93-
uncomment the STATIC_URL to point to the new URL.
94-
95-
The app can be deployed by running
96-
97-
gcloud app deploy
98-
99-
which deploys to version 1, and `promote` makes version 1 the default version.
100-
101-
Now you can view the admin panel of your deployed site at https://<your-app-id>.appspot.com/admin.
107+
Make sure to replace <your-cloud-bucket> within `mysite/settings.py` to set STATIC_URL to the
108+
correct value to serve static content from, and uncomment the STATIC_URL to point to the new URL.
102109

103110
### Production
104111

105112
Once you are ready to serve your content in production, there are several
106113
changes required for the configuration. Most notable changes are:
114+
115+
* Use Google Cloud Storage or a CDN to serve static files
107116
* Add ".appspot.com" to your `ALLOWED_HOSTS`
108117
* Change the `DEBUG` variable to `False` in your settings.py file.
109-
* If you are using a Cloud SQL database
110-
instance, in order to change from `DEBUG = True`
111-
to `DEBUG = False` you will need to properly configure the database. See
112-
instructions
113-
[here](https://cloud.google.com/sql/docs/app-engine-connect#gaev2-csqlv2) and be
114-
sure to change your `app.yaml` file as well as the `HOST` key in your
115-
`DATABASES` object.
116-
117118

118119
## Contributing changes
119120

appengine/flexible/django_cloudsql/app.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ runtime: python
33
vm: true
44
entrypoint: gunicorn -b :$PORT mysite.wsgi
55

6+
beta_settings:
7+
cloud_sql_instances: <your-cloudsql-connection-string>
8+
69
runtime_config:
710
python_version: 3
811
# [END runtime]

appengine/flexible/django_cloudsql/mysite/settings.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,18 @@
7878
DATABASES = {
7979
'default': {
8080
'ENGINE': 'django.db.backends.mysql',
81-
'NAME': '<your-database-name>',
82-
'USER': '<your-database-user>',
83-
'PASSWORD': '<your-database-password>',
84-
'HOST': '<your-database-host>',
85-
'PORT': '3306',
81+
'NAME': 'polls',
82+
'USER': 'root',
83+
'PASSWORD': '<your-root-password>',
8684
}
8785
}
86+
# In the flexible environment, you connect to CloudSQL using a unix socket.
87+
# Locally, you can use the CloudSQL proxy to proxy a localhost connection
88+
# to the instance
89+
DATABASES['default']['HOST'] = '/cloudsql/<your-cloudsql-connection-string>'
90+
if os.getenv('GAE_APPENGINE_HOSTNAME'):
91+
else:
92+
DATABASES['default']['HOST'] = '127.0.0.1'
8893
# [END dbconfig]
8994

9095
# Internationalization

appengine/flexible/django_cloudsql/mysite/urls.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@
1414

1515
from django.conf.urls import include, url
1616
from django.contrib import admin
17+
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
18+
from django.conf import settings
1719

1820
urlpatterns = [url(r'^', include('polls.urls')),
1921
url(r'^admin/', admin.site.urls)]
22+
23+
24+
# This enables static files to be served from the Gunicorn server
25+
# In Production, serve static files from Google Cloud Storage or an alternative
26+
# CDN
27+
if settings.DEBUG:
28+
urlpatterns += staticfiles_urlpatterns()

0 commit comments

Comments
 (0)