Git Product home page Git Product logo

gc's Introduction

Getting started with Django on Google Cloud Platform

This repository is an example of how to run a Django app on Google Managed VMs. It uses the Writing your first Django app as the example app to deploy.

Setup the database

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.

  • Create a CloudSQL instance

    • In the instances list, click your Cloud SQL instance.

    • Click Access Control.

    • In the IP address subsection, click Request an IPv4 address to enable access to the Cloud SQL instance through an IPv4 address. It will take a moment to initialize the new IP address.

    • Also under Access Control, in the Authorization subsection, under Allowed Networks, click the add (+) button .

    • In the Networks field, enter 0.0.0.0/0. This value allows access by all IP addresses.

    • Click Save.

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.

  • Alternatively, the instance can be created with the gcloud command line tool as follows, substituting `your-root-pw with a strong, unique password.

    gcloud sql instances create <instance_name> --assign-ip --authorized-networks=0.0.0.0/0 set-root-password=your-root-pw

  • Create a Database And User

    • 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.
      • From the CloudSQL instance in the console, click New user.
      • Enter a username and password for the application. For example, name the user "pythonapp" and give it a randomly generated password.
      • Click Add.
      • Click Databases and then click New database.
      • For Name, enter the name of your database (for example, "polls"), and click Add.

Once you have a SQL host, configuring mysite/settings.py to point to your database. Change your-database-name, your-database-user, your-database-host , and your-database-password to match the settings created above. Note the instance name is not used in this configuration, and the host name is the IP address you created.

Running locally

First make sure you have Django installed. It's recommended you do so in a virtualenv. The requirements.txt contains just the Django dependency.

pip install -r requirements.txt

Once the database is setup, run the migrations.

python manage.py migrate

If you'd like to use the admin console, create a superuser.

python manage.py createsuperuser

The app can be run locally the same way as any other Django app.

python manage.py runserver

Now you can view the admin panel of your local site at

http://localhost:8080/admin

Deploying

Since the Django development server is not built for production, our container uses the Gunicorn server. Since Gunicorn doesn't serve static content, the static content is instead served from Google Cloud Storage.

First, make a bucket and make it publically readable, replacing with a bucket name, such as your project id:

`gsutil mb gs://<your-gcs-bucket>`
`gsutil defacl set public-read gs://<your-gcs-bucket>`

Next, gather all the static content locally into one folder using the Django collectstatic command

`python manage.py collectstatic`

Then upload it to CloudStorage using the gsutil rsync command

`gsutil rsync -R static/ gs://<your-gcs-bucket>/static` 

Now your static content can be served from the following URL:

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

Make sure to replace within mysite/settings.py to set STATIC_URL to the correct value to serve static content from, and uncomment the STATIC_URL to point to the new URL.

The app can be deployed by running

`gcloud preview app deploy app.yaml --set-default --version=1 --promote`

which deploys to version 1, and promote makes version 1 the default version.

Now you can view the admin panel of your deployed site at

https://<your-app-id>.appspot.com/admin

Contributing changes

Licensing

gc's People

Contributors

pumbaacave avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.