A skeleton for building Python applications on Google App Engine with the Django Web Framework using Cloud SQL.
See our other Google Cloud Platform github repos for sample applications and scaffolding for other python frameworks and use cases.
Note that this project no lnoger uses the built-in Django library but
uses pip -t
to vendor Django 1.8 into the lib/ folder.
The basic skeleton of this project was created using the standard Djagno commands:
```
django-admin startproject myproject
```
The DATABASE field in the myproject/settings.py is modified to use CloudSQL locally, and through a Unix socket when deployed to AppEngine. The static file handler is also set up in the app.yaml.
This project assumes you are using CloudSQL as your database. CloudSQL is a managed MySQL instance hosted by Google Cloud, although you could also host a MySQL instance elsewhere and use that. For better scalability, consider Cloud Datastore, although you must use a custom backend such as Djangae for your models to work with it.
-
Install the App Engine Python SDK. See the README file for directions. You'll need python 2.7 and pip 1.4 or later installed too.
-
Clone this repo with
git clone https://github.com/GoogleCloudPlatform/appengine-try-python-django.git
-
Install dependencies in the project's lib directory. Note: App Engine can only import libraries from inside your project directory.
cd appengine-try-python-django pip install -r requirements.txt -t lib
-
Create a new CloudSQL instance.
- From the Google Cloud Console, go to Storage > CloudSQL> Create Instance
- Under Acces Control > IP Address > Request IPv4 Address. This address will be your HOST for remote access to the CloudSQL instance in mysite/settings.py, so replace with this address.
- Under Databaeses, click New database and create the name for your database in mysite/settings.py. Replace with this value.
- Under Access Control > Authorization > Allowed Networks, click Add item, and add Network 0.0.0.0/0. This opens up access to your CloudSQL instance from any network. Stricter firewall settings should be considered for production applications.
- Under Access Control > Users > Create user account. Create a username and password and edit settings.py DATABASES to reflect this. Replace and with these variables.
Note in myproject/settings.py, the deployed app does not use the above settings, but instead talks to the instance through a Unix socket as root. When testing locally, use the settings created above to access the database.
-
Run this project locally from the command line:
dev_appserver.py .
or just using the Django local server
python manage.py collectstatic python manage.py runserver
Visit the application http://localhost:8080
See the development server documentation for options when running dev_appserver.
To deploy the application:
-
Use the Admin Console to create a project/app id. (App id and project id are identical)
-
Collect the static files into the static/ directory
django-admin startproject mysite
-
Verify that and have been replaced in mysite/settings.py with your Cloud Project ID and your database name respectively.
-
appcfg.py -A <your-project-id> --oauth2 update .
or using the new gcloud SDK commands.
gcloud config set project <project name> # only required once gcloud preview app deploy --set-default
-
Congratulations! Your application is now live at your-app-id.appspot.com
See the Third party libraries page for libraries that are already included in the SDK. To include SDK libraries, add them in your app.yaml file. Other than libraries included in the SDK, only pure python libraries may be added to an App Engine project.
Alternatively, pure Python libraries can be added to requirements.txt, and
installed using pip -t
into the lib/ folder and accessed directly through
there.
Star this repo if you found it useful. Use the github issue tracker to give feedback on this repo.
See CONTRIB.md
See LICENSE