diff --git a/appengine/standard/django/README.md b/appengine/standard/django/README.md new file mode 100644 index 00000000000..ee13c8337b6 --- /dev/null +++ b/appengine/standard/django/README.md @@ -0,0 +1,10 @@ +# Getting started with Django on Google Cloud Platform on App Engine Standard + +This repository is an example of how to run a [Django](https://www.djangoproject.com/) +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. + + +# Tutorial +See our [Running Django in the App Engine Standard Environment](https://cloud.google.com/python/django/appengine) tutorial for instructions for setting up and deploying this sample application. diff --git a/appengine/standard/django/app.yaml b/appengine/standard/django/app.yaml new file mode 100644 index 00000000000..15781218755 --- /dev/null +++ b/appengine/standard/django/app.yaml @@ -0,0 +1,31 @@ +# [START django_app] +runtime: python27 +api_version: 1 +threadsafe: yes + +handlers: +- url: /static + static_dir: static/ +- url: .* + script: mysite.wsgi.application + +# Only pure Python libraries can be vendored +# Python libraries that use C extensions can +# only be included if they are part of the App Engine SDK +# Using Third Party Libraries: https://cloud.google.com/appengine/docs/python/tools/using-libraries-python-27 +libraries: +- name: MySQLdb + version: 1.2.5 +# [END django_app] + +# Google App Engine limits application deployments to 10,000 uploaded files per +# version. The skip_files section allows us to skip virtual environment files +# to meet this requirement. The first 5 are the default regular expressions to +# skip, while the last one is for all env/ files. +skip_files: +- ^(.*/)?#.*#$ +- ^(.*/)?.*~$ +- ^(.*/)?.*\.py[co]$ +- ^(.*/)?.*/RCS/.*$ +- ^(.*/)?\..*$ +- ^env/.*$ diff --git a/appengine/standard/django/appengine_config.py b/appengine/standard/django/appengine_config.py new file mode 100644 index 00000000000..f18f4328eb2 --- /dev/null +++ b/appengine/standard/django/appengine_config.py @@ -0,0 +1,19 @@ +# Copyright 2015 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START vendor] +from google.appengine.ext import vendor + +vendor.add('lib') +# [END vendor] diff --git a/appengine/standard/django/manage.py b/appengine/standard/django/manage.py new file mode 100755 index 00000000000..834b0091d73 --- /dev/null +++ b/appengine/standard/django/manage.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python +# Copyright 2015 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os +import sys + +if __name__ == "__main__": + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings") + + from django.core.management import execute_from_command_line + + execute_from_command_line(sys.argv) diff --git a/appengine/standard/django/mysite/__init__.py b/appengine/standard/django/mysite/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/appengine/standard/django/mysite/settings.py b/appengine/standard/django/mysite/settings.py new file mode 100644 index 00000000000..bd8b71a445f --- /dev/null +++ b/appengine/standard/django/mysite/settings.py @@ -0,0 +1,145 @@ +# Copyright 2015 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +""" +Django settings for mysite project. + +Generated by 'django-admin startproject' using Django 1.8.5. + +For more information on this file, see +https://docs.djangoproject.com/en/1.8/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/1.8/ref/settings/ +""" + +# Build paths inside the project like this: os.path.join(BASE_DIR, ...) +import os + +BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = '-c&qt=71oi^e5s8(ene*$b89^#%*0xeve$x_trs91veok9#0h0' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +# SECURITY WARNING: App Engine's security features ensure that it is safe to +# have ALLOWED_HOSTS = ['*'] when the app is deployed. If you deploy a Django +# app not on App Engine, make sure to set an appropriate host here. +# See https://docs.djangoproject.com/en/1.10/ref/settings/ +ALLOWED_HOSTS = ['*'] + +# Application definition + +INSTALLED_APPS = ( + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'polls', +) + +MIDDLEWARE_CLASSES = ( + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', + 'django.middleware.security.SecurityMiddleware', +) + +ROOT_URLCONF = 'mysite.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'mysite.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/1.8/ref/settings/#databases + +# [START db_setup] +if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine'): + # Running on production App Engine, so connect to Google Cloud SQL using + # the unix socket at /cloudsql/ + DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.mysql', + 'HOST': '/cloudsql/', + 'NAME': 'polls', + 'USER': '', + 'PASSWORD': '', + } + } +else: + # Running locally so connect to either a local MySQL instance or connect to + # Cloud SQL via the proxy. To start the proxy via command line: + # + # $ cloud_sql_proxy -instances=[INSTANCE_CONNECTION_NAME]=tcp:3306 + # + # See https://cloud.google.com/sql/docs/mysql-connect-proxy + DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.mysql', + 'HOST': '127.0.0.1', + 'PORT': '3306', + 'NAME': 'polls', + 'USER': '', + 'PASSWORD': '', + } + } +# [END db_setup] + +# Internationalization +# https://docs.djangoproject.com/en/1.8/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/1.8/howto/static-files/ + +STATIC_ROOT = 'static' +STATIC_URL = '/static/' diff --git a/appengine/standard/django/mysite/urls.py b/appengine/standard/django/mysite/urls.py new file mode 100644 index 00000000000..1b387da6467 --- /dev/null +++ b/appengine/standard/django/mysite/urls.py @@ -0,0 +1,23 @@ +# Copyright 2015 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from django.conf.urls import include, url +from django.contrib import admin + +from polls.views import index + +urlpatterns = [ + url(r'^$', index), + url(r'^admin/', include(admin.site.urls)), +] diff --git a/appengine/standard/django/mysite/wsgi.py b/appengine/standard/django/mysite/wsgi.py new file mode 100644 index 00000000000..ddc0aaa08b8 --- /dev/null +++ b/appengine/standard/django/mysite/wsgi.py @@ -0,0 +1,29 @@ +# Copyright 2015 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +""" +WSGI config for mysite project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings") + +application = get_wsgi_application() diff --git a/appengine/standard/django/polls/__init__.py b/appengine/standard/django/polls/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/appengine/standard/django/polls/admin.py b/appengine/standard/django/polls/admin.py new file mode 100644 index 00000000000..0c5b3a09a34 --- /dev/null +++ b/appengine/standard/django/polls/admin.py @@ -0,0 +1,19 @@ +# Copyright 2015 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from django.contrib import admin + +from .models import Question + +admin.site.register(Question) diff --git a/appengine/standard/django/polls/models.py b/appengine/standard/django/polls/models.py new file mode 100644 index 00000000000..9e69c2a93ff --- /dev/null +++ b/appengine/standard/django/polls/models.py @@ -0,0 +1,26 @@ +# Copyright 2015 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from django.db import models + + +class Question(models.Model): + question_text = models.CharField(max_length=200) + pub_date = models.DateTimeField('date published') + + +class Choice(models.Model): + question = models.ForeignKey(Question) + choice_text = models.CharField(max_length=200) + votes = models.IntegerField(default=0) diff --git a/appengine/standard/django/polls/tests.py b/appengine/standard/django/polls/tests.py new file mode 100644 index 00000000000..c3b029bad97 --- /dev/null +++ b/appengine/standard/django/polls/tests.py @@ -0,0 +1,20 @@ +# Copyright 2015 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Uncomment these imports and add tests here + +# from django import http +# from django.test import TestCase + +# from . import views diff --git a/appengine/standard/django/polls/views.py b/appengine/standard/django/polls/views.py new file mode 100644 index 00000000000..595ccf18ed8 --- /dev/null +++ b/appengine/standard/django/polls/views.py @@ -0,0 +1,19 @@ +# Copyright 2015 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from django.http import HttpResponse + + +def index(request): + return HttpResponse("Hello, world. You're at the polls index.") diff --git a/appengine/standard/django/requirements-vendor.txt b/appengine/standard/django/requirements-vendor.txt new file mode 100644 index 00000000000..b3e1f245ff9 --- /dev/null +++ b/appengine/standard/django/requirements-vendor.txt @@ -0,0 +1 @@ +Django==1.10.3 diff --git a/appengine/standard/django/requirements.txt b/appengine/standard/django/requirements.txt new file mode 100644 index 00000000000..ca504fefa53 --- /dev/null +++ b/appengine/standard/django/requirements.txt @@ -0,0 +1,3 @@ +# only install locally since GAE has its own version +MySQL-python==1.2.5 +Django==1.10.3