Skip to content

Commit 37a606c

Browse files
ryanmatsJon Wayne Parrott
authored andcommitted
UpdatedDjango GAE Standard Sample to CloudSQL v2 and Django 1.10 (#714)
1 parent df22328 commit 37a606c

File tree

15 files changed

+369
-0
lines changed

15 files changed

+369
-0
lines changed

appengine/standard/django/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Getting started with Django on Google Cloud Platform on App Engine Standard
2+
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
5+
[Writing your first Django app](https://docs.djangoproject.com/en/1.9/intro/tutorial01/) as the
6+
example app to deploy.
7+
8+
9+
# Tutorial
10+
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.

appengine/standard/django/app.yaml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# [START django_app]
2+
runtime: python27
3+
api_version: 1
4+
threadsafe: yes
5+
6+
handlers:
7+
- url: /static
8+
static_dir: static/
9+
- url: .*
10+
script: mysite.wsgi.application
11+
12+
# Only pure Python libraries can be vendored
13+
# Python libraries that use C extensions can
14+
# only be included if they are part of the App Engine SDK
15+
# Using Third Party Libraries: https://cloud.google.com/appengine/docs/python/tools/using-libraries-python-27
16+
libraries:
17+
- name: MySQLdb
18+
version: 1.2.5
19+
# [END django_app]
20+
21+
# Google App Engine limits application deployments to 10,000 uploaded files per
22+
# version. The skip_files section allows us to skip virtual environment files
23+
# to meet this requirement. The first 5 are the default regular expressions to
24+
# skip, while the last one is for all env/ files.
25+
skip_files:
26+
- ^(.*/)?#.*#$
27+
- ^(.*/)?.*~$
28+
- ^(.*/)?.*\.py[co]$
29+
- ^(.*/)?.*/RCS/.*$
30+
- ^(.*/)?\..*$
31+
- ^env/.*$
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright 2015 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START vendor]
16+
from google.appengine.ext import vendor
17+
18+
vendor.add('lib')
19+
# [END vendor]

appengine/standard/django/manage.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python
2+
# Copyright 2015 Google Inc. All rights reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
import os
17+
import sys
18+
19+
if __name__ == "__main__":
20+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
21+
22+
from django.core.management import execute_from_command_line
23+
24+
execute_from_command_line(sys.argv)

appengine/standard/django/mysite/__init__.py

Whitespace-only changes.
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# Copyright 2015 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""
16+
Django settings for mysite project.
17+
18+
Generated by 'django-admin startproject' using Django 1.8.5.
19+
20+
For more information on this file, see
21+
https://docs.djangoproject.com/en/1.8/topics/settings/
22+
23+
For the full list of settings and their values, see
24+
https://docs.djangoproject.com/en/1.8/ref/settings/
25+
"""
26+
27+
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
28+
import os
29+
30+
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
31+
32+
33+
# Quick-start development settings - unsuitable for production
34+
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/
35+
36+
# SECURITY WARNING: keep the secret key used in production secret!
37+
SECRET_KEY = '-c&qt=71oi^e5s8(ene*$b89^#%*0xeve$x_trs91veok9#0h0'
38+
39+
# SECURITY WARNING: don't run with debug turned on in production!
40+
DEBUG = True
41+
42+
# SECURITY WARNING: App Engine's security features ensure that it is safe to
43+
# have ALLOWED_HOSTS = ['*'] when the app is deployed. If you deploy a Django
44+
# app not on App Engine, make sure to set an appropriate host here.
45+
# See https://docs.djangoproject.com/en/1.10/ref/settings/
46+
ALLOWED_HOSTS = ['*']
47+
48+
# Application definition
49+
50+
INSTALLED_APPS = (
51+
'django.contrib.admin',
52+
'django.contrib.auth',
53+
'django.contrib.contenttypes',
54+
'django.contrib.sessions',
55+
'django.contrib.messages',
56+
'django.contrib.staticfiles',
57+
'polls',
58+
)
59+
60+
MIDDLEWARE_CLASSES = (
61+
'django.contrib.sessions.middleware.SessionMiddleware',
62+
'django.middleware.common.CommonMiddleware',
63+
'django.middleware.csrf.CsrfViewMiddleware',
64+
'django.contrib.auth.middleware.AuthenticationMiddleware',
65+
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
66+
'django.contrib.messages.middleware.MessageMiddleware',
67+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
68+
'django.middleware.security.SecurityMiddleware',
69+
)
70+
71+
ROOT_URLCONF = 'mysite.urls'
72+
73+
TEMPLATES = [
74+
{
75+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
76+
'DIRS': [],
77+
'APP_DIRS': True,
78+
'OPTIONS': {
79+
'context_processors': [
80+
'django.template.context_processors.debug',
81+
'django.template.context_processors.request',
82+
'django.contrib.auth.context_processors.auth',
83+
'django.contrib.messages.context_processors.messages',
84+
],
85+
},
86+
},
87+
]
88+
89+
WSGI_APPLICATION = 'mysite.wsgi.application'
90+
91+
92+
# Database
93+
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
94+
95+
# [START db_setup]
96+
if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine'):
97+
# Running on production App Engine, so connect to Google Cloud SQL using
98+
# the unix socket at /cloudsql/<your-cloudsql-connection string>
99+
DATABASES = {
100+
'default': {
101+
'ENGINE': 'django.db.backends.mysql',
102+
'HOST': '/cloudsql/<your-cloudsql-connection-string>',
103+
'NAME': 'polls',
104+
'USER': '<your-database-user>',
105+
'PASSWORD': '<your-database-password>',
106+
}
107+
}
108+
else:
109+
# Running locally so connect to either a local MySQL instance or connect to
110+
# Cloud SQL via the proxy. To start the proxy via command line:
111+
#
112+
# $ cloud_sql_proxy -instances=[INSTANCE_CONNECTION_NAME]=tcp:3306
113+
#
114+
# See https://cloud.google.com/sql/docs/mysql-connect-proxy
115+
DATABASES = {
116+
'default': {
117+
'ENGINE': 'django.db.backends.mysql',
118+
'HOST': '127.0.0.1',
119+
'PORT': '3306',
120+
'NAME': 'polls',
121+
'USER': '<your-database-user>',
122+
'PASSWORD': '<your-database-password>',
123+
}
124+
}
125+
# [END db_setup]
126+
127+
# Internationalization
128+
# https://docs.djangoproject.com/en/1.8/topics/i18n/
129+
130+
LANGUAGE_CODE = 'en-us'
131+
132+
TIME_ZONE = 'UTC'
133+
134+
USE_I18N = True
135+
136+
USE_L10N = True
137+
138+
USE_TZ = True
139+
140+
141+
# Static files (CSS, JavaScript, Images)
142+
# https://docs.djangoproject.com/en/1.8/howto/static-files/
143+
144+
STATIC_ROOT = 'static'
145+
STATIC_URL = '/static/'
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2015 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from django.conf.urls import include, url
16+
from django.contrib import admin
17+
18+
from polls.views import index
19+
20+
urlpatterns = [
21+
url(r'^$', index),
22+
url(r'^admin/', include(admin.site.urls)),
23+
]
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright 2015 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""
16+
WSGI config for mysite project.
17+
18+
It exposes the WSGI callable as a module-level variable named ``application``.
19+
20+
For more information on this file, see
21+
https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/
22+
"""
23+
24+
import os
25+
26+
from django.core.wsgi import get_wsgi_application
27+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
28+
29+
application = get_wsgi_application()

appengine/standard/django/polls/__init__.py

Whitespace-only changes.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright 2015 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from django.contrib import admin
16+
17+
from .models import Question
18+
19+
admin.site.register(Question)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copyright 2015 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from django.db import models
16+
17+
18+
class Question(models.Model):
19+
question_text = models.CharField(max_length=200)
20+
pub_date = models.DateTimeField('date published')
21+
22+
23+
class Choice(models.Model):
24+
question = models.ForeignKey(Question)
25+
choice_text = models.CharField(max_length=200)
26+
votes = models.IntegerField(default=0)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2015 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Uncomment these imports and add tests here
16+
17+
# from django import http
18+
# from django.test import TestCase
19+
20+
# from . import views
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright 2015 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from django.http import HttpResponse
16+
17+
18+
def index(request):
19+
return HttpResponse("Hello, world. You're at the polls index.")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Django==1.10.3
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# only install locally since GAE has its own version
2+
MySQL-python==1.2.5
3+
Django==1.10.3

0 commit comments

Comments
 (0)