google app engine ndb

https://developers.google.com/appengine/docs/python/ndb/overview

一般 GAE 是內建 datastore
但也可以連結 NDB(儲存於 cloud SQL, 介面是 django admin)
要使用 ndb 必須做以下設定

app.yaml


application: xxxx
version: 1
runtime: python27
api_version: 1
threadsafe: true

builtins:
- appstats: on
- admin_redirect: on
- remote_api: on
- deferred: on
- django_wsgi: on

inbound_services:
- warmup

libraries:
- name: jinja2                                                                
  version: latest
- name: django
  version: latest

handlers:
- url: /media
  static_dir: media
  expiration: "1d"

- url: /

  static_dir: media
  expiration: "30s"

- url: /smedia
  static_dir: media
  expiration: "365d"

- url: /static
  static_dir: static/
  expiration: "30s"

- url: /tagtoo_ad.js
  static_files: media/tagtoo_ad.js
  upload: media/tagtoo_ad.js
  expiration: "1d"

- url: /favicon\.ico
  static_files: media/favicon.ico
  upload: media/favicon.ico
  expiration: "365d"
  mime_type: image/vnd.microsoft.icon

- url: /admin/.*
  script: wsgi.application

- url: /.*
  script: index.app

專案的根目錄資料夾要放幾個檔案:

A. wsgi.py
內容:


"""
WSGI config for tagtoo_admin project.

This module contains the WSGI application used by Django's development server
and any production WSGI deployments. It should expose a module-level variable
named ``application``. Django's ``runserver`` and ``runfcgi`` commands discover
this application via the ``WSGI_APPLICATION`` setting.

Usually you will have the standard Django WSGI application here, but it also
might make sense to replace the whole Django WSGI application with a custom one
that later delegates to the Django one. For example, you could introduce WSGI
middleware here, or combine a Django application with an application of another
framework.

"""
import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")

# This application object is used by any WSGI server configured to use this
# file. This includes Django's development server, if the WSGI_APPLICATION
# setting points here.
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

# Apply WSGI middleware here.
# from helloworld.wsgi import HelloWorldApplication
# application = HelloWorldApplication(application)


B. manage.py


#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)


C. settings.py  -->  包含連到 cloud SQL 的帳密, django admin 
其中重點是要設定
ROOT_URLCONF = 'urls'



代表 url 設定要去urls.py 裏頭找

D. 接著是 urls.py


from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Uncomment the admin/doc line below to enable admin documentation:
    url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    url(r'^admin/', include(admin.site.urls)),
)

留言

熱門文章