You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: appengine/flexible/django_cloudsql/README.md
+51-50Lines changed: 51 additions & 50 deletions
Original file line number
Diff line number
Diff 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
2
2
3
3
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.
5
7
6
8
7
9
## Setup the database
8
10
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.
10
13
14
+
* Create a [Second Generation CloudSQL instance](https://cloud.google.com/sql/docs/create-instance)
11
15
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.
13
17
14
-
* In the instances list, click your Cloud SQL instance.
18
+
* Install the [CloudSQL proxy](https://cloud.google.com/sql/docs/sql-proxy)
15
19
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>.
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:
28
30
29
-
* Alternatively, the instance can be created with the gcloud command line tool as follows, substituting `your-root-pw
* 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
33
35
34
-
*Create a Database And User
36
+
*Optionally, use the root account to create a new MySQL user.
35
37
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'@'%';" ``
43
40
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.
47
44
48
45
## Running locally
49
46
@@ -55,6 +52,8 @@ contains just the Django dependency.
55
52
56
53
Once the database is setup, run the migrations.
57
54
55
+
python manage.py makemigrations
56
+
python manage.py makemigrations polls
58
57
python manage.py migrate
59
58
60
59
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.
65
64
66
65
python manage.py runserver
67
66
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
69
68
70
69
## Deploying
71
70
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
+
74
81
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:
76
91
77
92
gsutil mb gs://<your-gcs-bucket>
78
93
gsutil defacl set public-read gs://<your-gcs-bucket>
@@ -89,31 +104,17 @@ Now your static content can be served from the following URL:
0 commit comments