2012年10月16日 星期二

GAE - Use Django Framework - from scratch 2

Continue GAE - Using Django Framework - from scratch 1

Use View to say 'Hello World!'

  1. Modify urls.py, add a line to urlpatterns, like below:
    urlpatterns = patterns('',
        url(r'^$', 'views.sayhello')
    )
  2. Add a new python module views.py under project root, and with following content :
    from django.http import HttpResponse
    def sayhello(request):
        return HttpResponse("Hello world !")

Add Hello App of Django and handle by template

  1. Under project root, gdjtest, run : manage.py startapp hello
  2. Modify urls.py, add a line to urlpatterns, like below:
    urlpatterns = patterns('',
        url(r'^$', 'views.sayhello')
        url(r'^hello/(?P[a-zA-Z0-9_.-]+)/$', 'hello.views.sayhello')
    )
  3. New a folder under project root, named 'templates', for template files storage, and modify setting.py for template storage setting as below:
    TEMPLATE_DIRS = (os.path.join(os.path.dirname(__file__), 'templates') )
  4. New a html template file under folder templates, named hello.html and add line inside html >body< block : 
    Hello {{name}}!
  5. New a python module, views.py, under the new app, hello, folder, and add code as below:
    from django.shortcuts import render_to_response
        def sayhello(request, name):
        return render_to_response('hello.html', {'name':name})
  6. Test : use browser to access 'http://localhost:8080/hello/sunnylin/', and you will get response :
    Hello sunnylin!

Reference :

2012年10月15日 星期一

GAE - Use Django Framework - from scrach 1

Pre-requsite

  1. Install eclipse 3.8 Platform Runtime Binary
    http://download.eclipse.org/eclipse/downloads/drops/R-3.8-201206081200/
  2. Install Aptana Studio 3 eclipse plugin : http://download.aptana.com/studio3/plugin/install
  3. Install Google App Engine
  4. Install Python 2.7.2, 
  5. Install Django 1.3, don't install 1.4.x that still not supported by GAE untill now.
  6. Download Django-nonrel engine (djangoappengin)
  7. Add %python2.7_home%\Scripts to path (that we need to access dango-admin.py)
Create a new test project
  1. Under eclipse workspace directory run : django-admin.py startproject gdjtest
  2. New a 'PyDev Google App Engine Project' from eclipse, named 'gdjtest'
  3. New a GEA configuration file under project root : app.yaml, with content as below
    application: gdjtest
    version: 1
    runtime: python27
    api_version: 1
    threadsafe: yes

    libraries:
    - name: django
    version: 1.3

    - url: /.*
    script: djangoappengine.main.application
  4. Copy djangoappengin from django-nonrel to project root directory
  5. Start up GAE Django server : project submenu:[Run As][PyDev:Google App Run]
  6. Use browser to test : http://localhost:8080/
  7. If success, got the page response as below :

    It worked!

    Congratulations on your first Django-powered page.

    Of course, you haven't actually done any work yet. Here's what to do next:
    • If you plan to use a database, edit the DATABASES setting in settings/settings.py.
    • Start your first app by running python settings/manage.py startapp [appname].
    You're seeing this message because you have DEBUG = True in your Django settings file and you haven't configured any URLs. Get to work!

Error Debugging

  1. App server startup fail : No URLMap entries found in application configuration
    app.yaml loss handlers section
    handlers:
    - url: /.*
    script: djangoappengine.main.application
  2. ImportError: No module named django.conf
    app.yaml lose django libraries
    libraries:
    - name: django
    version: 1.3
  3. ImportError: No module named gdjtest.urls
    Modify settings.py, find line ROOT_URLCONF = 'gdjtest.urls' and change to ROOT_URLCONF = 'urls'

Complete app.yaml for Django on GAE:

application: optionwatch
version: 1
runtime: python27
api_version: 1
threadsafe: yes

builtins:
- remote_api: on

inbound_services:
- warmup

libraries:
- name: django
version: 1.3
- name: lxml
version: latest

handlers:
- url: /_ah/queue/deferred
script: djangoappengine.deferred.handler.application
login: admin

- url: /_ah/stats/.*
script: djangoappengine.appstats.application

- url: /media/admin
static_dir: django/contrib/admin/media
expiration: '0'

- url: /.*
script: djangoappengine.main.application


2012年10月12日 星期五

GAE - Python Development Test 2 - Using Django Framework

Pre-request:

  1. Python2.7 is installed
  2. Google App Engine SDK is install (Here version 1.7.2 is used)
  3. Add the google_appengine folder(ex. d:\Google\google_appengine)to your PATH
  4. Django 1.3 is installed (Don't use 1.4.x, the latest version of Dgango, because GAE support 1.3 only on the time of this post)
  5. Add python scripts to PATH, that should include django-admin.py (ex. d:\dev\python2.7\Scripts)
PS. This post is tested under Windows environment.

Create GAE project with Django (1.3 supported, currently) enabled:

  1. Create a new Google App Engine project:
    menu[File:New:Project...] => dialog:tree[PyDev-PyDev Google App Engine Project]:button[next] => dialog:label[Project Nam:text["gtest"]:button[next] => dialog:label[Google App Engine Directory]:button[Browser] => file_browser[D:\Dev\Google\google_appengine] :dialog:button[OK]:button[Next] => label[What's the application id ...]:text["gtest"]:label[From which template ...]:list[Empty Project]:button[Finish]
  2. Download Django-norel related zip file, and extract them,  for Django enabled on GAE, that did use non-RDB (bigtable) as backend database)
    1. django-nonrel : Django-nonrel is an attempt at fixing the problems and simplifying NoSQL development by adding support for NoSQL DBs to Django's ORM.
    2. djangoappengine : Contains all App Engine backends for Django-nonrel (database, email, etc.).
    3. djangotoolbox : A sandbox for Django-nonrel extra features.
    4. django-autoload : django-autoload is a reuseable django app which allows for correct initialization of your django project.
    5. django-dbindexer : Simplify your NoSQL model code and get advanced SQL-like features on non-relational databases.
    6. django-testapp
  3. Copy folders to project root directory from all above :
    1. Copy django-nonrel/django to %project%/django
    2. Copy djangotoolbox/djangotoolbox to %project%/djangotoolbox
    3. Copy django-autoload/autoload to %project%/autoload
    4. Copy django-dbindexer/dbindexer to /dbindexer
    5. Copy djangoappengine to /djangoappengine link
    6. Copy django-testapp/*.py & *.yaml to %project%/
    7. Copy django-testapp/template to %project/template
  4. Test run testapp in GAE and with Django enabled
    right click on project:menu[Run As][1.PyDev:Google App Run]
    console message:
    INFO 2012-10-12 07:00:33,980 dev_appserver_multiprocess.py:655] Running application dev~ctst on port 8080: http://localhost:8080
    INFO 2012-10-12 07:00:33,980 dev_appserver_multiprocess.py:657] Admin console is available at: http://localhost:8080/_ah/admin
  5. Start up browser and access http://localhost:8080, and can get first line message :

    It works!

    Your Django-nonrel installation is working fine. Now go and build something useful with it. :

2012年10月9日 星期二

GAE - Python Development Test 1 - Webapp Hello World

  1. Create a new Google App Engine project:
    menu[File:New:Project...] => dialog:tree[PyDev-PyDev Google App Engine Project]:button[next] => dialog:label[Project Nam:text["gtest"]:button[next] => dialog:label[Google App Engine Directory]:button[Browser]  => file_browser[D:\Dev\Google\google_appengine] :dialog:button[OK]:button[Next] => label[What's the application id ...]:text["gtest"]:label[From which template ...]:list[Hello Webapp World]:button[Finish]
  2. Test Run :
    click mouse on project named, gtest:right click:menu[Run As:PyDev : Google App Run]
  3. Modify app.yaml to use python2.7
    runtime: python => runtime: python27
    add : threadsafe: no (Note. if set threadsafe: yes will cause error, still don't know how to code for threadsafe)
  4. Test from browser:
    open browser with url : http://localhost:8080/  ==> Get result on page:"Hello, webapp World!"
  5. Setup project propertity:
    1. [project test]:right click => properities => Resource : label: [Text Encoding]: list select:Other[UTF-8]

Ref : 雲端運算-Google-App-Engine-GAE-程式開發入門-for-Python

2012年10月6日 星期六

GAE - Python Development Environment Setup


  1. Install Python 2.7.3 : http://www.python.org/download/releases/2.7.3/
    Insall path : D:\Dev\Python2.7
  2. Install Google App Engine for Python : https://developers.google.com/appengine/downloads?hl=zh-tw#Google_App_Engine_SDK_for_Python
    Installed version :GoogleAppEngine-1.7.2.msi
    Install path : D:\Dev\Google\google_appengine\
    Note : In the first page of setup starting, there is a notice that remind to use python 2.5, never mind, we just use python 2.7, and it is supported.
  3. Install eclipse : http://www.eclipse.org/download/
    Installed version : eclipse-java-juno-SR1-win32-x86_64.zip (here 64 bit version is used)
    Install path : D:\Dev\eclipse
  4. Install eclipse python development plugin: PyDev:
    eclipse update site : http://pydev.org/updates
    1. menu:Help=> Install New Software ...
    2. dialog:label[Work with]:text:"http://pydev.org/updates"=> button:[add]
    3. dialog:lable[Name]:text:"PyDev":label[Location]:text:"http://pydev.org/updates" => button[ok]
    4. checkbox:PyDev:button[Next]=>[Next]=> radio:accept => finish => adialog:checkbox[Aptana...]:button[Accept] => Restart eclipse
    5. Setup
      1. Setup Python Interpreter
        menu:Windows:Perferences => dialog:tree:PyDev:Editor:Intepreter - Python => [Auto Config] =>select:python2.7 => [ok]
      2. Setup workspace encodeing for Chinese compatible
        menu:Windows:Perferences => dialog:tree:Workspace => label[Text for encoding]:radio[Others]:list[UTF-8]