Serverless Application

Saturday, June 21, 2008

Step by Step guide to create and launch a Web Application with Google App Engine, Python and Django
<Part One>

Step zero: Download the software and Set up the environment
Following steps are required only Once to set up the Google App Engine/Python/Django development environment in Windows
  1. Download and install the latest Python (I'm using Python 2.5) from the Python website
  2. Download and install the latest Python pywin32 add-on http://sourceforge.net/projects/pywin32/
  3. Download the App Engine SDK. Follow the instructions on the download page to install the SDK on your computer.
  4. Download the Google App Engine Helper for Django at http://code.google.com/p/google-app-engine-django.
  5. Extract the helper source archive into a directory, c:downloadappengine_helper_for_django
Here are the recommended steps to create a new Application

Step one: Start the default server in localhost
  1. Sign up for an App Engine account and create an Application. i.e smartwebnote http://smartwebnote.appspot.com/
  2. Copy the c:downloadappengine_helper_for_django into the your working directory, c:demo and rename the folder appengine_helper_for_django to whatever the name you want i.e. coolsite.
  3. cd c:democoolsite
  4. c:democoolsite> manage.py runserver and go check the application running http://127.0.0.1:8080/
  5. In the command line, you may see
    "WARNING:root:Could not read datastore data from c:...localtempdjango_google-app-engine-django.datastore"
    Ignore the WARING for now
Now, You have a basic server running in your local PC!

Step two: Create an application and Handle the http request with "Hello World" response
  1. C:democoolsite>manage.py startapp smartwebnote
  2. Edit urls.py to add
    urlpatterns = patterns('smartwebnote.views',
    (r'^$', 'index'),
    )
  3. cd smartwebnote. Edit views.py
    from django.http import HttpResponse
    def index(request):
    return HttpResponse('Hello world')
  4. refresh the server at http://127.0.0.1:8080/ you should get the "Hello World"!
Step three: Add Models and play with the Development Console(replacement for Django Admin Site)
  1. Edit models.py
    from google.appengine.ext import db
    from appengie_django.models import BaseModel

    class Note(BaseModel):
    content = db.TextProperty()
    last_updated = db.DateTimeProperty(auto_now=True)
  2. Create a few Note objects using the Python shell
    > manage.py shell
    >>> from smartwebnote.models import Note
    >>> note = Note(content='my first note')
    >>> note.save()
    datastore_types.Key.from_path('Note', 1, _app=u'google-app-engine-django')
    >>> note = Note(content='my second note')
    >>> note.save()
    datastore_types.Key.from_path('Note', 2, _app=u'google-app-engine-django')
    >>> results = Note.all().fetch(limit=10)
    >>> print results
    [<smartwebnote.models.Note object at 0x01DFEA70>, <smartwebnote.models.Note object at 0x01DFEB10>]
  3. We have successfully created Note objects using shell. Now let's use the Development Console manage the objects
  4. goto: http://localhost:8080/_ah/admin/datastore. Click the interactive Console. Add the following code
    from smartwebnote.models import Note
    note = Note(content='my first note')
    note.save()
    print note
  5. go back to the Datastore Viewer and search for 'Note'. You should see the object you just created! Using the console to create a few more objects and try the delete function as well.
The Development console is not as powerful as the Django Admin. However it's better than nothing :)

Step four: Add Views and Templates to display the list
Views in Django = Controller in MVC pattern
Templates in Django = View in MVC pattern
The generic views provided by Django can not be supported in Google App Engine
  1. Test the query in the Application Interactive Console
    object_list = Note.all()
    for o in object_list:
    print o.content
    You should get a list of objects you created in the Console
  2. Edit the urls.py and add the pattern for list
    urlpatterns = patterns('smartwebnote.views',
    (r'^$', 'index'),
    (r'^list/$', 'list'),
    )
  3. Edit the smartwebnote/views.py
    query = Note.all()
    def list(request):
    return render_to_response('smartwebnote/note_list.html',
    {'object_list':query})
  4. Create templates/smartwebnote under c:/demo/coolsite/
  5. Create note_list.html

    <html>
    <body>
    <p> <a href ="create"> Create a new note</a></p>
    <table id="myTable">
    <thead>
    <tr>
    <th>Delete</th>
    <th>Note</th>
    <th>Last Updated</th>
    </tr>
    </thead>
    <tbody>
    {% for note in object_list %}
    <tr valign="top">
    <td>
    <a href="delete/{{ note.id }}">Delete</a>
    </td>
    <td>
    <a href="{{ note.id }}">{{ note.content }}</a>
    </td>
    <td>
    {{ note.last_updated|date:"M j, Y, P" }}
    </td>
    </tr>

    {% endfor %}
    </tbody>
    </table>

    </body>
    </html>
  6. Reload the page: http://127.0.0.1:8080/list/
You should see a list of Notes created before!
This completes the Part one of this tutorial. Hopefully this helps. Feel free to contact me if you have questions and comments.