Django and Google App Engine Tutorial
Posted by Peter Sankauskas on February 17th, 2009
Hi have been playing around with Google App Engine for a while now, and wanted to learn Django on a new application I am building. The two articles I started with are:
Both Django and GAE are being developed as I write this, so although these instructions are kind of recent, they are already out of date, or rely on you having knowledge of Django. Since there are a lot of others with no Python or Django experince wanting to learn, I thought I would make a tutorial that works as of today, but who knows a month from now or even tomorrow.
Note: This tutorial is written for Linux. Mac/Windows users will have to translate
Create django.zip
Django comes as a .tar.gz file, but we want a zip file to take advantage of the Zipimport library, so some conversion is needed.
- Download the latest Django and untar the file (in this case, it is 1.0.2)
- cd Django-1.0.2-final
- Create the zip file, but without the admin section since App Enging supplies it’s own
zip -r ~/django.zip django -x ‘django/contrib/admin*’
Get the Helper
The App Engine Helper or Django is an open source bootstrapper for getting Django started on App Engine. Downloading it from the website will currently give you quite an old version (r52) which will not work in this tutorial. Instead, use subversion to get the latest (r74).
- cd ~
- svn export http://google-app-engine-django.googlecode.com/svn/trunk/ gae-django-tutorial
- cd gae-django-tutorial
- mv ~/django.zip .
Setup App Engine
As you would with any GAE application, edit the app.yaml file to refer to your application ID. The Helper also needs to know where your Google App Engine SDK is, since it is going to change how you start the development server, so create a link to it:
- cd gae-django-tutorial
- ln -s /path/to/google_appengine .google_appengine
Start the development server
Django has a different way of running the development server. Instead of using dev_appserver.py to start the dev server, do the following:
- python manage.py runserver
If everything is running correctly, you should see something like:
INFO:root:Server: appengine.google.com INFO:root:Checking for updates to the SDK. INFO:root:The SDK is up to date. INFO:root:Running application google-app-engine-django on port 8000: http://localhost:8000
However, if you are like me and saw an error like the following:
ImportError: No module named antlr3
Then you will need to install the antlr3 python module. Luckily this is easy.
- Go to http://www.antlr.org/download/Python and download the latest zip
- unzip antlr_python_runtime-3.1.zip
- cd antlr_python_runtime-3.1
- sudo python setup.py install
Lets see the site! When you go to http://localhost:8000/ you should see a page saying “It worked! Congratulations on your first Django-powered page.”
Pretty (un)impressive huh?
Ok, now lets start doing something. Kill the server by pressing Ctrl-C. The Django tutorial is the next stop, which involves creating the Polls Django app. You can read through there to get a full understanding. For simplicity, I am only what I did to get it working.
- python manage.py startapp polls
- cd polls
- Edit models.py
from appengine_django.models import BaseModel from google.appengine.ext import db class Poll(BaseModel): question = db.StringProperty() pub_date = db.DateTimeProperty('date published') class Choice(BaseModel): poll = db.ReferenceProperty(Poll) choice = db.StringProperty() votes = db.IntegerProperty() - Edit views.py (notice that order_by() from the Django tutorial is just order() here)
from django.template import Context, loader from polls.models import Poll from django.http import HttpResponse def index(request): latest_poll_list = Poll.objects.all().order('-pub_date')[:5] t = loader.get_template('polls/index.html') c = Context({ 'latest_poll_list': latest_poll_list, }) return HttpResponse(t.render(c)) - cd ..
- Edit urls.py and add to urlpatterns
(r’^polls/’, ‘polls.views.index’), - mkdir -p templates/polls
- cd templates/polls
- Create 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 %} - cd ../..
- Edit settings.py and add to INSTALLED_APPS
‘polls’, - Now you can run the dev server again:
python manage.py runserver - And go to:
http://localhost:8000/polls/
- You should see the message “No polls are available.”
Adding some data
You can go through the rest of the Django Tutorial to fill out the rest of the views. One last thing to know is the admin site. To view it, go to:
Notice that the Datastore Viewer is empty – it doesn’t even know about our Poll or Choice Models. Don’t panic. Go back to your terminal, and terminate the dev server. Now run:
- python manage.py shell
This brings up a special python shell with the Django environment set up for you. Now you can run:
>>> from polls.models import Poll, Choice
>>> import datetime
>>> p = Poll(question=”What’s up?”, pub_date=datetime.datetime.now())
>>> p.save()
You have just created a new Poll. To end the shell, press Ctrl-D.
If you start the dev server again (or if you never stopped it in the first place), you should be able to go to:
http://localhost:8000/polls/
to see you new Poll, and go to the admin site:
http://localhost:8000/_ah/admin/datastore
to see and create new Polls
Easy huh?

February 20th, 2009 at 7:57 pm
Nice clear write up, just what I was looking for, thanks! Now to see if I can put this into practice.
February 21st, 2009 at 7:26 pm
For those people on Windows, who get an AttributeError when they visit http://localhost:8000 , take a look at http://code.google.com/p/google-app-engine-django/issues/detail?id=101 for a fix.
March 6th, 2009 at 2:55 pm
Thank you so much for writing this!
A note to super newbies – if you’re developing on Windows, add/edit an environmental variable called “PYTHONPATH” and give it a list of necessary directories as the value surrounded and separated by semicolons, e.g.:
;C:\flup-1.0.1;C:\Progra~1\Google\google_appengine\;
You may need to restart or log off/log on to see changes. Then you can skip the part about creating a link to google_appengine (step 3.2).
To get it onto the server, just do a “path/to/appcfg.py update gae-django-tutorial/” where appcfg.py is in the google_appengine directory.
April 10th, 2009 at 4:25 am
Deploying Django on AppEngine you should have a look on the appEnginePatch project.
It has zip, auth, registration, admin and more features already integrated. There is a ready made sample with django registration you can download and try.
Detailed questions how to deploy native Django on appEngine are discussed in app engine patch group.
Konrad
February 18th, 2010 at 7:28 pm
This got Django working for me on Google App Engine. Thanks!
March 6th, 2010 at 11:35 am
Thanks for this post. I am new at django and this was a big help.
March 6th, 2010 at 3:23 pm
Cool Thanks for this post. I am just starting python and this will help a lot.