2016年5月4日水曜日

GAE/P + Django1.8 + djangae に挑戦!

一応、動作を確認できたので、忘れないうちに書き込んでおきます。

1.構成

こんなカンジで配置していきます。

gae/
├ google_appengine/ … GAE の SDK です。
├ venv/
│ └ … Django1.8 … プロジェクト作成用です。
└ project/
  ├ mysite/
  │ ├ settings.py
  │ ├ wsgi.py
  │ └ urls.py
  ├ home/
  ├ lib/ … GAE にアップロードするライブラリの置き場です。
  │ └ Django1.8, djangae,
  ├ appengine_config.py
  ├ app.yaml

2.SDK のインストール

こちらからダウンロードして解凍します。
https://cloud.google.com/appengine/downloads

$ cd
$ mkdir gae
$ cd ~/gae
$ wget https://storage.googleapis.com/appengine-sdks/featured/google_appengine_1.9.36.zip
$ unzip google_appengine_1.9.36.zip
$ rm google_appengine_1.9.36.zip

3.Django プロジェクトの作成

3-1.仮想環境にDjango1.8をインストールします。

$ cd ~/gae
$ virtualenv venv
$ source ~/gae/venv/bin/activate
V pip install django==1.8.7

3-2.仮想環境内に、プロジェクトとアプリケーションを作成します。

V django-admin.py startproject mysite
V mv mysite project
V cd project
V python manage.py startapp home

※ プロジェクト名(project)とアプリケーション名(home)は仮です。

4.ライブラリの追加

GAE にアップロードするライブラリを /lib にインストールします。

V cd ~/gae/project
V mkdir lib
V pip install -t lib Django==1.8.7 djangae
V deactivate

5.各種設定

$ cd ~/gae/project

5-1. mysite/settings.py (変更)

① 最初に、これ。

# encoding: UTF-8
from djangae.settings_base import *

② インストール・アプリケーション

INSTALLED_APPS = (
 'djangae', #1行目に追加
 〜
 'djangae.contrib.gauth.datastore', #最終行に追加 Google認証用
)

③ ミドルウェア

MIDDLEWARE_CLASSES = (
 'google.appengine.ext.appstats.recording.AppStatsDjangoMiddleware', #1行目に追加 appstats用
 'djangae.contrib.security.middleware.AppEngineSecurityMiddleware', #追加
 〜
 #'django.contrib.auth.middleware.AuthenticationMiddleware', #コメントアウト
 'djangae.contrib.gauth.middleware.AuthenticationMiddleware', #追加 Google認証用
 〜
)

④ データーベース

DATABASES = {
 'default': {
  'ENGINE': 'djangae.db.backends.appengine',
 }
}

⑤ 最後に、これ。

from djangae.contrib.gauth.settings import *  # 最終行に追加

5-2. mysite/wsgi.py (変更)

--
# encoding: UTF-8
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
#application = get_wsgi_application()  #コメントアウト
from djangae.wsgi import DjangaeApplication #2行追加
application = DjangaeApplication(get_wsgi_application())
--

5-3. appengine_config.py (新規作成)

こちらを参考に。
http://d.hatena.ne.jp/noazoh/20140924/1411553513

--
# encoding: UTF-8
import os
import sys
import logging
BASE_DIR = os.path.dirname(__file__)
LIB_DIR = os.path.join(BASE_DIR, 'lib') #ライブラリの置き場所
sys.path.append(LIB_DIR)
sys.path.append(os.path.join(LIB_DIR, "dateutil"))
from google.appengine.ext import vendor
vendor.add('lib')
--

5-4. app.yaml (新規作成)

こちらを参考に。
https://cloud.google.com/appengine/docs/python/config/appref

--
application: myapp
version: alpha-001
runtime: python27
api_version: 1
threadsafe: true
builtins:
- appstats: on
handlers:
- url: /static/admin/
  static_dir: lib/django/contrib/admin/static/admin/
  secure: always
- url: /admin.*
  script: mysite.wsgi.application
  secure: always
  login: admin
- url: /.*
  script: mysite.wsgi.application
--

6.動作確認

$ cd ~/gae/project
$ python ../google_appengine/dev_appserver.py ./

ブラウザで確認します。
① http://localhost:8080  => おなじみの It worked! が表示されれば成功です。
② http://localhost:8080/admin/  => Django administration を使えます。
③ http://localhost:8080/_ah/stats/  => appstats を使えます。素晴らしい!!
④ http://localhost:8000  => SDK で コンソールも使えます。

7.所感など

なんとか、ここまでたどり着きました。
Web等で情報を探しても、なかなか見つからなかったけど、とりあえず、やりたいことは出来そうなカンジです。

引き続き、models とか、Sitemaps とか、試してみます。

8.おまけ

mysite/settings.py
--
# encoding: UTF-8

from djangae.settings_base import *

# 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&k%d%s84me^(2i^hq7e$x6*b%93#wfn!n5kfjzn65+e4yw-5'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
'djangae',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
#
'djangae.contrib.gauth.datastore',
)

MIDDLEWARE_CLASSES = (
'google.appengine.ext.appstats.recording.AppStatsDjangoMiddleware',
'djangae.contrib.security.middleware.AppEngineSecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
#'django.contrib.auth.middleware.AuthenticationMiddleware',
'djangae.contrib.gauth.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

DATABASES = {
'default': {
'ENGINE': 'djangae.db.backends.appengine',
}
}


# 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_URL = '/static/'

from djangae.contrib.gauth.settings import *
--

0 件のコメント:

コメントを投稿