Sunday, August 17, 2008

Django tutorial polls sample App -Google App Engine port

This article explains how to create a 'Google App Engine' version port of the 'polls' sample showed in Django tutorial: (http://www.djangoproject.com/documentation/tutorial01/).

After learning Python and finishing the official Django tutorial, I am trying to revise sample web app for Google App engine. Because Django does not support the database used by Google app engine, we need to revise all the codes related to database operation to deploy original web app on Google engine. The Google app engine use nonrelation database ' Big Table' instead of relation databases (Mysql, SQLlite…). If you want to learn more about the fundamental characteristics of 'Big table' to make your web app more efficiently, here are some useful article and video

Bigtable: A Distributed Storage System for Structured Data

http://labs.google.com/papers/bigtable.html

This article written by googlers. Looks like many Google applications are based on 'Big Table', like Google map, Google earth, etc.

Google I/O: Under the Covers of the Google App Engine Datastore

http://sites.google.com/site/io/under-the-covers-of-the-google-app-engine-datastore

1.Install Google app engine Django helper

First please install Google App engine SDK. Please install Google app engine Django helper ( link ).

http://code.google.com/p/google-app-engine-django/

2.Preloaded polls

Some polls are added for testing like following:

Poll:
Question1 (key name q1 )

Choice:
Choice_q1_c1 (key name q1_c1)
Choice_q1_c2 (key name q1_c2)
Choice_q1_c3 (key name q1_c3)
Choice_q1_c4 (key name q1_c4)

When a new entity is created, its key name can be set as a specific string value, but id value will be arbitrary integer. Entity can be retrieved through Model class method get_by_key_name without query.

3.main.py

# Standard Python imports.

import os

import sys

import logging

from appengine_django import InstallAppengineHelperForDjango

InstallAppengineHelperForDjango()

# Google App Engine imports.

from google.appengine.ext.webapp import util

# Import the part of Django that we use here.

import django.core.handlers.wsgi

def main():

# Create a Django application for WSGI.

application = django.core.handlers.wsgi.WSGIHandler()

# Run the WSGI CGI handler with that application.

util.run_wsgi_app(application)

if __name__ == '__main__':

main()

  • This code is from Google app engine Django helper.

4.url patterns

from django.conf.urls.defaults import *

urlpatterns = patterns('mysite.polls.views',

(r'^polls/$', 'index'),

(r'^polls/(?P<poll_id>\d+)/$', 'detail'),

(r'^polls/(?P<poll_id>\d+)/results/$', 'results'),

(r'^polls/(?P<poll_id>\d+)/vote/$', 'vote'),

)

from django.conf.urls.defaults import *

urlpatterns = patterns('polls.views',

(r'^polls/$', 'index'),

(r'^polls/(?P<poll_key_name>\w+)/$', 'detail'),

(r'^polls/(?P<poll_key_name>\w+)/results/$', 'results'),

(r'^polls/(?P<poll_key_name>\w+)/vote/$', 'vote'),

)

  • Since poll_key_name will be string like q1,q2 ... instead of integer ,'\w' is used to capture poll_key_name.

5.models.py

from django.db import models

class Poll(models.Model):

question = models.CharField(maxlength=200)

pub_date = models.DateTimeField('date published')

class Choice(models.Model):

poll = models.ForeignKey(Poll)

choice = models.CharField(maxlength=200)

votes = models.IntegerField()

from google.appengine.ext import db

from appengine_django.models import BaseModel

class Poll(BaseModel):

question = db.TextProperty()

pub_date = db.DateTimeProperty(auto_now_add=1)

def __str__(self):

return self.question

class Choice(BaseModel):

poll = db.ReferenceProperty(Poll)

choice = db.StringProperty()

votes = db.IntegerProperty(default = 0)

  • Datastore api has different datatype syntax .
  • '.ReferenceProperty' is used to build one to many relationship between Poll and Choice.

You can find out more information about building models relationships form this presentation

Working with Google App Engine Models

http://sites.google.com/site/io/working-with-google-app-engine-models

6.views

6.1 index.html

{% if latest_poll_list %}

<ul>

{% for poll in latest_poll_list %}

<li>{{ poll.question }}</li>

{% endfor %}

</ul>

{% else %}

<p>No polls are available.</p>

{% endif %}

{% if latest_poll_list %}

<ul>

{% for poll in latest_poll_list %}

<li>{{ poll.question }}</li>

{% endfor %}

</ul>

{% else %}

<p>No polls are available.</p>

{% endif %}

  • For this template, two version are the same.

6.2 function index()

from django.shortcuts import render_to_response

from mysite.polls.models import Poll

def index(request):

latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5]

return render_to_response('polls/index.html', {'latest_poll_list': latest_poll_list})

from django.shortcuts import render_to_response

from models import Poll

def index(request):

latest_poll_list = Poll.all().order('-pub_date').fetch(5)

return render_to_response('index.html', {'latest_poll_list': latest_poll_list})

  • The only difference is query syntax.

6.3 detail.html

<h1>{{ poll.question }}</h1>

{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}

<form action="/polls/{{ poll.id }}/vote/" method="post">

{% for choice in poll.choice_set.all %}

<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />

<label for="choice{{ forloop.counter }}">{{ choice.choice }}</label><br />

{% endfor %}

<input type="submit" value="Vote" />

</form>

<h1>{{ poll.question }}</h1>

{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}

<form action="/polls/{{ poll.key.name }}/vote/" method="post">

{% for choice in choice_list %}

<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.key.name }}" />

<label for="choice{{ forloop.counter }}">{{ choice.choice }}</label><br />

{% endfor %}

<input type="submit" value="Vote" />

</form>

  • Looks like there is no way to achieve the same effect as 'poll.choice_set.all' in Django template without introduce parameters. Let provide 'choice_list ' instead.

6.4 function detail()

from django.http import Http404

# ...

def detail(request, poll_id):

try:

p = Poll.objects.get(pk=poll_id)

except Poll.DoesNotExist:

raise Http404

return render_to_response('polls/detail.html', {'poll': p})

from django.http import Http404

from models import Poll,Choice

# ...

def detail(request, poll_key_name):

p=Poll.get_by_key_name(poll_key_name)

if bool(p)==False:

raise Http404

else:

return render_to_response('detail.html', {'poll': p,'choice_list':p.choice_set.fetch(10)})

  • Google Datastore will return 'nonetype' object when no entity exits for a given keyname. And no exception will be raised. we can take advantage of the fact that the boolean value of 'none' type is False.
  • If Http404 is not raised for non-exist entity, AttributeError for Nonetype error will appear sooner or later.

6.5 function vote()

from django.shortcuts import get_object_or_404, render_to_response

from django.http import HttpResponseRedirect

from mysite.polls.models import Choice, Poll

# ...

def vote(request, poll_id):

p = get_object_or_404(Poll, pk=poll_id)

try:

selected_choice = p.choice_set.get(pk=request.POST['choice'])

except (KeyError, Choice.DoesNotExist):

# Redisplay the poll voting form.

return render_to_response('polls/detail.html', {

'poll': p,

'error_message': "You didn't select a choice.",

})

else:

selected_choice.votes += 1

selected_choice.save()

# Always return an HttpResponseRedirect after successfully dealing

# with POST data. This prevents data from being posted twice if a

# user hits the Back button.

return HttpResponseRedirect('/polls/%s/results/' % p.id)

from django.http import Http404

from django.shortcuts import get_object_or_404, render_to_response

from django.http import HttpResponseRedirect

from django.core.urlresolvers import reverse

from models import Poll,Choice

# ...

def vote(request, poll_key_name):

p=Poll.get_by_key_name(poll_key_name)

if bool(p)==False:

raise Http404

selected_choice = Choice.get_by_key_name(request.POST['choice'])

if bool(selected_choice)==False:

# Redisplay the poll voting form.

return render_to_response('detail.html', {

'poll': p,

'error_message': "You didn't select a choice.",

})

else:

selected_choice.votes += 1

selected_choice.save()

# Always return an HttpResponseRedirect after successfully dealing

# with POST data. This prevents data from being posted twice if a

# user hits the Back button.

return HttpResponseRedirect(reverse('polls.views.results', args=(p.key().name(),)))

  • Please note vote default value should be given or 'selected_choice.votes += 1' will raise exception.

6.6 results.html

<h1>{{ poll.question }}</h1>

<ul>

{% for choice in poll.choice_set.all %}

<li>{{ choice.choice }} -- {{ choice.votes }} vote{{ choice.votespluralize }}</li>

{% endfor %}

</ul>

<h1>{{ poll.question }}</h1>

<ul>

{% for choice in choice_list %}

<li>{{ choice.choice }} -- {{ choice.votes }} vote{{ choice.votespluralize }}</li>

{% endfor %}

</ul>

  • It is similar to detail.html. Real list of choice ('choice_list') is needed.

from django.http import Http404

from django.shortcuts import get_object_or_404, render_to_response

def results(request, poll_id):

p = get_object_or_404(Poll, pk=poll_id)

return render_to_response('polls/results.html', {'poll': p})

from django.http import Http404
from django.shortcuts import render_to_response

def results(request, poll_key_name):
p=Poll.get_by_key_name(poll_key_name)
if bool(p)==False:
raise Http404
else:
return render_to_response('results.html', {'poll': p,'choice_list':p.choice_set.fetch(10)})

7. source code and live webapp

you can find live app at

http://visachoice.appspot.com/polls/

for different polls, please check

http://visachoice.appspot.com/polls/q1/

http://visachoice.appspot.com/polls/q2/

....

source code can be found at

http://code.google.com/p/djangopollsgae/downloads/list

your comment are welcome!!

I just updated an Immigration Guide website http://visachoice.appspot.com/ with Google App Engine+Django+Guido's codereview sourcecode.